StreamReader path changes automatically

淺唱寂寞╮ 提交于 2019-12-11 02:27:13

问题


I have some strange problem (for me).

There is an application which is a windows form application "firstapp.exe". There is another application which is windows form application too "launcher.exe". And there is a console application with name "server.exe".

Both firstapp and launcher are in the same directory. In that directory there is also a "Config" folder with some other files in it.

The code which I use to read one file from config folder in firstapp:

StreamReader reader = new StreamReader("Config\\launcher.txt");
string readed_config = reader.ReadToEnd();
reader.Close();

If I run the firstapp application with launcher (using process.start) all goes fine. When I run it with console application, which is not in the same directory as firstapp I get the "directory not found exception" from that part of code (posted above).

How can I solve the problem? Why is console application adding its own path to another application which should run independently?


回答1:


Sounds like you need to set the WorkingDirectory property of your Process before calling Process.Start.

string launcherPath = @"C:\SomePathToLauncher\Launcher.exe";
myProcess.StartInfo.FileName = launcherPath;
myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(launcherPath);
myProcess.Start();



回答2:


 StreamReader reader = new StreamReader("Config\\launcher.txt");

Never use hard-coded relative file paths in your code. It critically depends on Environment.CurrentDirectory and that's way too unpredictable. External code can kill you as you found out. Internal code as well, use OpenFileDialog and your code will crash. You can always get a full path with Assembly.GetEntryAssembly().Location and the Path class:

var exedir = Path.GetDirectory(Assembly.GetEntryAssembly().Location);
var path = Path.Combine(exedir, @"Config\launcher.txt");
using (var reader = new StreamReader(path)) {
    //...
}

Now it always works.




回答3:


It's because your path is relative and the Current Working Directory is different when the console app kicks off your winform. Also, you should wrap the stream reader in a using statement. As it stands, unless you explicitly call Dispose() elsewhere in your code you're holding on to resources that should be freed.

To fix your problem either change the WorkingDirectory when you start the process using Process.StartInfo.WorkingDirectory or change the path in your code so it is not relative. Or another option would be to pass the path to the application or read it from a resource file so that it can be given the appropriate path when it executes.




回答4:


the answer is in the question. you are saying that "When I run it with console application, which is not in the same directory". if it's not in the same directory how will it find a directory "Config" if it diesn't exist there. make sure that the directory exist there



来源:https://stackoverflow.com/questions/17930475/streamreader-path-changes-automatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!