EagerToLearn
EagerToLearn

Reputation: 857

PATH environment variable and Relative File Paths

Prologue - If i append PATH environment variable in windows with Installation Directory path of my application, i don't need to CD to installation directory to execute it.

Question - Would relative file path(s) in my application interpreted according to current execution path in console or according to installation directory. Strangely, in my application, the paths are being interpreted relative to current execution path, thus causing exceptions (File not found, etc).

Please help me out.

Upvotes: 1

Views: 6151

Answers (2)

yas4891
yas4891

Reputation: 4862

The behaviour you are encountering (relative pathes being evaluate in the context of the current working directory) is by design.

If you want to always place a file next to the currently executing assembly, this piece of code might come in handy:

public static string GetPathRelativeToExecutingAssemblyLocation(string aRelativePath)
{
    return Path.Combine(
        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), 
        aRelativePath);
}

Upvotes: 1

Oded
Oded

Reputation: 498904

Relative paths will be interpreted relative to Environment.CurrentDirectory.

It will default to the directory where the process started in, but can be changed.

Upvotes: 3

Related Questions