CA1302:请不要对区域设置特定的字符串进行硬编码

类型名

DoNotHardcodeLocaleSpecificStrings

CheckId

CA1302

类别

Microsoft.Globalization

是否重大更改

非重大更改

原因

方法使用字符串表示特定系统文件夹路径的一部分。

规则说明

System.Environment.SpecialFolder 枚举包含表示特殊系统文件夹的成员。 对于不同的操作系统,这些文件夹的位置可能具有不同的值,用户也可能会更改某些位置,或者这些位置已经进行了本地化。 例如,System 文件夹就是一个特殊文件夹,该文件夹在 Windows XP 中为“C:\WINDOWS\system32”,而在 Windows 2000 中为“C:\WINNT\system32”。 Environment.GetFolderPath 方法返回与 Environment.SpecialFolder 枚举关联的位置。 GetFolderPath 返回的位置已进行了本地化,以便与目前运行的计算机相适应。

该规则将使用 GetFolderPath 方法检索的文件夹路径标记到不同的目录级别。 每个字符串都会与这些标记进行比较。 如果找到匹配项,则假定该方法正在生成一个与该标记关联的表示系统位置的字符串。 为了确保可移植性和可本地化性,请使用 GetFolderPath 方法来检索特殊文件夹的位置,而不要使用字符串。

如何解决冲突

要修复与该规则的冲突,请使用 GetFolderPath 方法检索位置。

何时禁止显示警告

如果字符串不用于表示与 Environment.SpecialFolder 枚举关联的某个系统位置,则可以安全地禁止显示此规则发出的警告。

示例

下面的示例生成一个指向常规应用程序数据文件夹的路径,它将根据此规则产生三个警告。 然后,该示例使用 GetFolderPath 方法检索该路径。

Imports System

Namespace GlobalizationLibrary

   Class WriteSpecialFolders

      Shared Sub Main()

         Dim string0 As String = "C:"

         ' Each of the following three strings violates the rule.
         Dim string1 As String = "\Documents and Settings"
         Dim string2 As String = "\All Users"
         Dim string3 As String = "\Application Data"
         Console.WriteLine(string0 & string1 & string2 & string3)

         ' The following statement satisfies the rule.
         Console.WriteLine(Environment.GetFolderPath( _ 
            Environment.SpecialFolder.CommonApplicationData))

      End Sub

   End Class

End Namespace
using System;

namespace GlobalizationLibrary
{
   class WriteSpecialFolders
   {
      static void Main()
      {
         string string0 = "C:";

         // Each of the following three strings violates the rule.
         string string1 = @"\Documents and Settings";
         string string2 = @"\All Users";
         string string3 = @"\Application Data";
         Console.WriteLine(string0 + string1 + string2 + string3);

         // The following statement satisfies the rule.
         Console.WriteLine(Environment.GetFolderPath(
            Environment.SpecialFolder.CommonApplicationData));
      }
   }
}

相关规则

CA1303:不要将文本作为本地化参数传递