GetEnvironmentVariable() and SetEnvironmentVariable() for PATH Variable

后端 未结 6 1651

I want to extend the current PATH variable with a C# program. Here I have several problems:

  1. Using GetEnvironmentVariable(\"PATH\", EnvironmentVariable

6条回答
  •  抹茶落季
    2020-12-11 15:37

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

提交回复
热议问题