Process.Start filename using %temp%

后端 未结 5 1887
孤街浪徒
孤街浪徒 2021-01-20 04:48

For some odd reaseon this code fails:

p.StartInfo.FileName = @\"%temp%\\SSCERuntime_x86-ENU.msi\";

and this code succes:

p.         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-20 05:22

    The Process class does not expand strings with environment variables (i.e. %temp%).

    If you want to use environment variables to build the FileName property then you'll have to get the environment variables (using the GetEnvironmentVariable method on the Environment class) and perform the substitution yourself, like so:

    // Construct the path.
    string temp = Environment.GetEnvironmentVariable("temp");
    string path = Path.Combine(temp, "SSCERuntime_x86-ENU.msi");
    
    // Launch the process.
    Process p = new Process();
    p.StartInfo.FileName = path;
    p.StartInfo.Arguments = "/passive";
    p.Start();
    

    Additionally, you can use the ExpandEnvironmentVariables method with your original string like so:

    p.StartInfo.FileName = 
        Environment.ExpandEnvironmentVariables(@"%temp%\SSCERuntime_x86-ENU.msi");
    

提交回复
热议问题