Reputation: 1945
In my Visual Studio console application my project is located in the below location:
c:\MyProject\FilesToUpload\test.txt
I am trying to access this file using the following code:
string path = Path.Combine(Environment.CurrentDirectory, @"FilesToUpload\", "test.txt");
But this path returns: c:\MyProject\bin\debug\net461\FilesToUpload\test.txt
The path I would like to get is: c:\MyProject\FilesToUpload\test.txt
Upvotes: 3
Views: 3333
Reputation: 19151
Environment.CurrentDirectory
will return the path from which your program is currently running. What you will need to do is decide on one of the following:
c:\MyProject\FilesToUpload\
In the first case, you could use something like this:
var relation = @"..\..\..\FilesToUpload\";
var currPath = Path.Combine(Environment.CurrentDirectory, relation, "test.txt");
Update / clarification:
For the first option, in your case (your example), using a relative path should effectively change your returned path from:
c:\MyProject\bin\debug\net461\FilesToUpload\test.txt
To:
c:\MyProject\bin\debug\net461\..\..\..\FilesToUpload\test.txt
Try this, and see how if it works - If the path is not exactly correct, you should be able to figure out how to adjust it.
Upvotes: 3
Reputation: 453
"Environment.CurrentDirectory" return correct path. Your application is an "exe" file but not source files. And it runs from a compile output directory. You don't need to get file from the "c:\MyProject\FilesToUpload\test.txt" but you to need change "Copy to Output Directory" property of the "test.txt" to "Copy Always".
Upvotes: 1