Reputation: 83
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.
Upvotes: 0
Views: 274
Reputation: 3888
you should replace your "777.txt"
with AppDomain.CurrentDomain.BaseDirectory & "777.txt"
Upvotes: 1
Reputation: 6374
Please try the following:
ProcessStartInfo startInfo = new ProcessStartInfo(@"c:\app1\app1.exe");
startInfo.WorkingDirectory= @"c:\app1";
Process.Start(startInfo);
Upvotes: 1
Reputation: 40139
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.
Upvotes: 1