1

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.

2 Answers 2

3

Relative paths will be interpreted relative to Environment.CurrentDirectory.

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.