简体   繁体   English

Process.start环境路径

[英]Process.start environment path

I have console application 1 which write text to file and it is in C:/app1 我有将文本写入文件的控制台应用程序1,它位于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 我还有另一个控制台应用程序2,c:/ app2,它启动了控制台应用程序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. 出于某种原因,当我运行应用程序2时,输出777.txt将位于folder2中,而不是folder1中。 When I run application 1 from windows explorer, the output 777.txt will be in folder1. 当我从Windows资源管理器中运行应用程序1时,输出777.txt将位于folder1中。

I look and tried to add environment.path but it didn't solve the problem. 我查看并尝试添加environment.path,但是它没有解决问题。

Your application 1 is using a relative path, not a rooted path. 您的应用程序1使用的是相对路径,而不是根路径。 That path is relative to the "current directory", not the "path" environment variable. 该路径是相对于“当前目录”的,而不是相对于“ path”环境变量的。

Process can accept a ProcessStartInfo instance which includes a property to define the current directory. Process可以接受ProcessStartInfo实例,该实例包含定义当前目录的属性。 You'll want to set that to the location of application 1 before starting it. 您需要在启动之前将其设置为应用程序1的位置。

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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