Set environment variables for a process

后端 未结 2 724
时光取名叫无心
时光取名叫无心 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:16

    There are many types of environment variables, like system level and users. It depends on your requirements.

    For setting environment variables, just use the Get Set method. Pass variables Name and Value as parameters and if use to define access level then must pass with it. For accessing the value then use the Set method to pass the access level parameter too.

    For example, I am defining user-level variables:

    //For setting and defining variables
    System.Environment.SetEnvironmentVariable("PathDB", txtPathSave.Text, EnvironmentVariableTarget.User);
    System.Environment.SetEnvironmentVariable("DBname", comboBoxDataBaseName.Text, EnvironmentVariableTarget.User);
    
    //For getting
    string Pathsave = System.Environment.GetEnvironmentVariable("PathDB", EnvironmentVariableTarget.User);
    string DBselect = System.Environment.GetEnvironmentVariable("DBname", EnvironmentVariableTarget.User);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题