Although accessing these file paths in code is pretty straightforward, there is a small caveat to it. A lot of users use the System.Environment.CurrentDirectory

This works well in most situations. Note how the tooltip states - "the directory from which this process starts".
However, if your application contains any piece of code that can alter the current working directory - say, you use a OpenFileDialog control to pick a file on the file system but do not restore the working directory by setting the RestoreDirectory property, this results in changing the current working directory and the quoted portion of the tooltip no longer holds good.
I would recommend using the Application.StartupPath instead of the System.Environment.CurrentDirectory. The folder path returned by this function is not altered during the course of execution of the application.

Console applications do not have access to the Application object and hence you might need to use the System.Reflection.Assembly.GetExecutingAssembly.Location method to get the full path of the console application. You can then use the IO.Path.GetDirectoryName method to extract the application launch directory.

No comments:
Post a Comment