I want to extend the current PATH variable with a C# program. Here I have several problems:
Using GetEnvironmentVariable(\"PATH\", EnvironmentVariable
While working on the application we had to have an option to use Oracle instantclient from user-defined folder. In order to use the instantclient we had to modify the environment path variable and add this folder before calling any Oracle related functionality. Here is method that we use for that:
///
/// Adds an environment path segments (the PATH varialbe).
///
/// The path segment.
public static void AddPathSegments(string pathSegment)
{
LogHelper.Log(LogType.Dbg, "EnvironmentHelper.AddPathSegments", "Adding path segment: {0}", pathSegment);
string allPaths = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
if (allPaths != null)
allPaths = pathSegment + "; " + allPaths;
else
allPaths = pathSegment;
Environment.SetEnvironmentVariable("PATH", allPaths, EnvironmentVariableTarget.Process);
}
Note that this has to be called before anything else, possibly as the first line in your Main file (not sure about console applications).