Skip to content

Gendarme.Rules.Portability.DoNotHardcodePathsRule(2.10)

Sebastien Pouliot edited this page Jan 22, 2011 · 2 revisions

DoNotHardcodePathsRule

Assembly: Gendarme.Rules.Portability
Version: 2.10

Description

This rule checks for strings that contain valid paths, either under Unix or Windows file systems. Path literals are often not portable across operating systems (e.g. different path separators). To ensure correct cross-platform functionality they should be replaced by calls to Path.Combine and/or Environment.GetFolderPath.

Examples

Bad example:

void ReadConfig ()
{
    using (FileStream fs = File.Open ("~/.local/share/myapp/user.config")) {
        // read configuration
    }
}

Good example:

void ReadConfig ()
{
    string config_file = Environment.GetFolderPath (SpecialFolder.LocalApplicationData);
    config_file = Path.Combine (Path.Combine (config_file, "myapp"), "user.config");
    using (FileStream fs = File.Open (config_file)) {
        // read configuration
    }
}

Notes

  • This rule is available since Gendarme 2.0
Clone this wiki locally