简体   繁体   中英

Process.start environment path

I have console application 1 which write text to file and it is in C:/app1

using (StreamWriter k = new StreamWriter("777.txt"))
    k.WriteLine("aa");

I have another console application 2, c:/app2, which start console application 1

System.Diagnostics.Process.Start("c:/app1/app1.exe");

For some reason, when I run application 2, the output 777.txt will be in folder2 instead of folder1. When I run application 1 from windows explorer, the output 777.txt will be in folder1.

I look and tried to add environment.path but it didn't solve the problem.

Your application 1 is using a relative path, not a rooted path. That path is relative to the "current directory", not the "path" environment variable.

Process can accept a ProcessStartInfo instance which includes a property to define the current directory. You'll want to set that to the location of application 1 before starting it.

Please try the following:

        ProcessStartInfo startInfo = new ProcessStartInfo(@"c:\app1\app1.exe");
        startInfo.WorkingDirectory= @"c:\app1";

        Process.Start(startInfo);

您应该将"777.txt"替换为AppDomain.CurrentDomain.BaseDirectory & "777.txt"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM