次の方法で共有


CA1302: Do not hardcode locale specific strings

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
RuleId CA1302
Category Microsoft.Globalization
Breaking change Non-breaking

Cause

A method uses a string literal that represents part of the path of certain system folders.

Note

This rule has been deprecated. For more information, see Deprecated rules.

Rule description

The System.Environment.SpecialFolder enumeration contains members that refer to special system folders. The locations of these folders can have different values on different operating systems, the user can change some of the locations, and the locations are localized. An example of a special folder is the System folder, which is "C:\WINDOWS\system32" on Windows XP but "C:\WINNT\system32" on Windows 2000. The System.Environment.GetFolderPath method returns the locations that are associated with the Environment.SpecialFolder enumeration. The locations that are returned by GetFolderPath are localized and appropriate for the currently running computer.

This rule tokenizes the folder paths that are retrieved by using the GetFolderPath method into separate directory levels. Each string literal is compared to the tokens. If a match is found, it is assumed that the method is building a string that refers to the system location that is associated with the token. For portability and localizability, use the GetFolderPath method to retrieve the locations of the special system folders instead of using string literals.

How to fix violations

To fix a violation of this rule, retrieve the location by using the GetFolderPath method.

When to suppress warnings

It is safe to suppress a warning from this rule if the string literal is not used to refer to one of the system locations that is associated with the Environment.SpecialFolder enumeration.

Example

The following example builds the path of the common application data folder, which generates three warnings from this rule. Next, the example retrieves the path by using the GetFolderPath method.

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));
      }
   }
}
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

CA1303: Do not pass literals as localized parameters