I am currently porting a C# application from .NET Framework to .NET Core, to be able to run it on Linux. At some point this application starts another program in a separate process, using the System.Diagnostics.Process
class. While this works well in general, that particular programm needs to be run with a modified PATH
environment variable. This is what I did for Windows (paths changed for testing on Linux):
string directory = "/tmp";
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
Arguments = "-c \"echo $PATH\"",
FileName = "/bin/bash",
};
processStartInfo.EnvironmentVariables["PATH"] += ";" + directory;
Process.Start(processStartInfo);
Unfortunately Linux uses a colon (":") as a path separator, thus the program produces
/usr/local/bin:/usr/bin:/bin:/usr/games;/tmp
instead of the desired output
/usr/local/bin:/usr/bin:/bin:/usr/games:/tmp
My question now is: How do I properly update the PATH
variable in a safe, cross-platform way? Is there some system-dependent property to retrieve the separator character (like Environment.NewLine
for line breaks or Path.DirectorySeparatorChar
for slashes), or do I have to check for the current OS manually?