Set environment variables for a process

后端 未结 2 737
时光取名叫无心
时光取名叫无心 2020-11-30 03:31

What is the environment variable concept?

In a C# program I need to call an executable. The executable will call some other executables that reside in the same folde

2条回答
  •  盖世英雄少女心
    2020-11-30 04:21

    What is your problem actually? System.Environment.SetEnvironmentVariable changes the environment variables of the current process. If you want to change the variables of a process you create, just use the EnvironmentVariables dictionary property:

    var startInfo = new ProcessStartInfo();
    
    // Sets RAYPATH variable to "test"
    // The new process will have RAYPATH variable created with "test" value
    // All environment variables of the created process are inherited from the
    // current process
    startInfo.EnvironmentVariables["RAYPATH"] = "test";
    
    // Required for EnvironmentVariables to be set
    startInfo.UseShellExecute = false;
    
    // Sets some executable name
    // The executable will be search in directories that are specified
    // in the PATH variable of the current process
    startInfo.FileName = "cmd.exe";
    
    // Starts process
    Process.Start(startInfo);
    

提交回复
热议问题