Doctorslo
Doctorslo

Reputation: 287

StreamReader path changes automatically

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?

Upvotes: 2

Views: 1493

Answers (4)

evanmcdonnal
evanmcdonnal

Reputation: 48096

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.

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941605

 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.

Upvotes: 2

No Idea For Name
No Idea For Name

Reputation: 11577

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

Upvotes: 0

keyboardP
keyboardP

Reputation: 69372

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();

Upvotes: 5

Related Questions