我有一些奇怪的问题(对我来说)。
有一个应用程序是 Windows 窗体应用程序“firstapp.exe”。 还有另一个应用程序也是 Windows 窗体应用程序“launcher.exe”。 并且有一个名为“server.exe”的控制台应用程序。
firstapp 和 launcher 都在同一个目录下。在该目录中还有一个“Config”文件夹,其中包含一些其他文件。
我用来从 firstapp 的配置文件夹中读取一个文件的代码:
StreamReader reader = new StreamReader("Config\\launcher.txt");
string readed_config = reader.ReadToEnd();
reader.Close();
如果我使用启动器(使用 process.start)运行 firstapp 应用程序,一切正常。 当我使用控制台应用程序运行它时,它与 firstapp 不在同一个目录中,我从那部分代码(上面发布的)中得到“找不到目录异常”。
我该如何解决这个问题? 为什么控制台应用程序将自己的路径添加到另一个应该独立运行的应用程序?
最佳答案
听起来您需要设置 WorkingDirectory在调用 Process.Start
之前您的 Process 属性。
string launcherPath = @"C:\SomePathToLauncher\Launcher.exe";
myProcess.StartInfo.FileName = launcherPath;
myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(launcherPath);
myProcess.Start();
关于c# - StreamReader 路径自动更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17930475/