001
001

Reputation: 65077

Where is my Application path?

I added a folder "myFolder" to my visual studio project, how do I execute and read the file from the current location???

.\ <-- this does not seem to work?

1)

    System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
    start.FileName = @".\myFolder\app.exe" + " -q " + "http://www.google.com " + " output.pdf";
    start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

etc...etc...

2)

    FileStream fs = File.OpenRead(@".\myFolder\output.pdf");

Upvotes: 0

Views: 382

Answers (2)

Crippledsmurf
Crippledsmurf

Reputation: 4012

Folders created using the solution explorer are not copied to the output directory (where the executable lives) by default unless an item that has been added to the project in that directory has the Copy To Output Directory value set to something other than Do Not Copy so if all you did was create an empty directory using the solution explorer, then its likely the directory does not exist in the output path.

This code would create a directory at the output path of the application

 var directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "YourDirectoryName");
        if (!Directory.Exists(directoryPath))
            Directory.CreateDirectory(directoryPath);

Note that sicnificant exception handling is not included in this code, I/O is one of the more exception prone activities one can perform so make sure you have sufficient error handling present.

If you wanted to have the directory created as part of the build process you could either run mkdir as an external build command or look at using the MakeDir MSBuild Task

Upvotes: 1

Nathan Taylor
Nathan Taylor

Reputation: 24606

You can use System.AppDomain.CurrentDomain.BaseDirectory to get the directory which contains the binary. From there you can navigate to your subdirectory.

Make sure you select 'Copy to Output Directory' in the properties window for the files in the folder you added to the project, that will make sure they are copied to the bin directory when you compile your application.

Upvotes: 1

Related Questions