title | description | ms.date | ms.topic | f1_keywords | helpviewer_keywords | author | ms.author | manager | ms.subservice | dev_langs | monikerRange | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CA1302: Do not hardcode locale specific strings |
A method uses a string literal that represents part of the path of certain system folders. |
11/04/2016 |
reference |
|
|
mikejo5000 |
mikejo |
mijacobs |
code-analysis |
|
vs-2019 |
Item | Value |
---|---|
RuleId | CA1302 |
Category | Microsoft.Globalization |
Breaking change | Non-breaking |
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.
The xref:System.Environment.SpecialFolder?displayProperty=fullName 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". The xref:System.Environment.GetFolderPath%2A?displayProperty=fullName method returns the locations that are associated with the xref:System.Environment.SpecialFolder enumeration. The locations that are returned by xref:System.Environment.GetFolderPath%2A are localized and appropriate for the currently running computer.
This rule tokenizes the folder paths that are retrieved by using the xref:System.Environment.GetFolderPath%2A 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 xref:System.Environment.GetFolderPath%2A 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 by using the xref:System.Environment.GetFolderPath%2A 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 that is associated with the xref:System.Environment.SpecialFolder enumeration.
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 xref:System.Environment.GetFolderPath%2A method.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_CodeAnalysis/FxCop.Globalization.HardcodedLocaleStrings/cs/FxCop.Globalization.HardcodedLocaleStrings.cs" id="Snippet1":::