The first code works:

private const string _File = @"C:\Program Files\Folder\File.txt"; 

The second code seems to be correct, but does not work:

 private const string _File = "\"%ProgramFiles%\\Folder\\File.txt\""; 

Tell me how to fix the second code so that the x32 system has a path:

C:\Program Files\Folder\File.txt

And in the x64 system is the same (without x86)

C:\Program Files\Folder\File.txt

    1 answer 1

    The file API naturally knows nothing about environment variables. Therefore, you need to get their value. The easiest way is to use the Environment.ExpandEnvironmentVariables() method, which resolves all the environment variables in a given string:

     private const string _File = "\"%ProgramFiles%\\Folder\\File.txt\""; var path = Environment.ExpandEnvironmentVariables(_File); 

    There is also the Environment.GetEnvironmentVariable() method, which returns the value for the specified variable.

    • The idea came that the path is not needed at all, if you create a file next to the application: @"File.txt" - Vitokhv
    • one
      @Vitokhv is generally the most correct implementation - create files in the user's directory, because there may be no write access to the Program Files or to the folder with the application. - andreycha