Skip to content

Latest commit

 

History

History
66 lines (46 loc) · 3.21 KB

ca1302.md

File metadata and controls

66 lines (46 loc) · 3.21 KB
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
DoNotHardcodeLocaleSpecificStrings
CA1302
DoNotHardcodeLocaleSpecificStrings
CA1302
mikejo5000
mikejo
mijacobs
code-analysis
CSharp
VB
vs-2019

CA1302: Do not hardcode locale specific strings

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 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.

How to fix violations

To fix a violation of this rule, retrieve the location by using the xref:System.Environment.GetFolderPath%2A 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 xref:System.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 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":::

:::code language="vb" source="../snippets/visualbasic/VS_Snippets_CodeAnalysis/FxCop.Globalization.HardcodedLocaleStrings/vb/FxCop.Globalization.HardcodedLocaleStrings.vb" id="Snippet1":::

Related rules

CA1303: Do not pass literals as localized parameters