Do not hardcode locale specific strings







Breaking Change

Non Breaking

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

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 Environment.GetFolderPath method returns the locations associated with the Environment.SpecialFolder enumeration. The locations returned by GetFolderPath are localized and appropriate for the currently running computer.

This rule tokenizes the folder paths retrieved using the GetFolderPath method into separate directory levels. Each string literal is compared to the tokens and, if a match is found, it is assumed that the method is building a string that refers to the system location 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.

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

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 associated with the Environment.SpecialFolder enumeration.

The following example builds a path to the common application data folder, which generates three warnings from this rule. Next, the example retrieves the path 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.

Community Additions