简体   繁体   中英

Path.Combine not working / cannot get working directory

I've set my working directory to a folder called "working" within my solution (by right-clicking solution->debug->set working directory). I've created a file in that folder called appSettings.xml. I want to get the path of that xml file and put it into a variable, I've tried the following code but it doesn't work, what am I doing wrong?:

EDIT: the problem appears to be that my working folder is ...bin\\debug even though I've set it to the correct path in the properties. How do I set it to a different directory?

string settingsPath = Path.Combine(Directory.GetCurrentDirectory(), "appSettings.xml");

First of all check, Directory.GetCurrentDirectory() is exist or not

if(Directory.Exists(Directory.GetCurrentDirectory())
{
  //This will write CurrentDirectory path on console
   Console.WriteLine(Directory.GetCurrentDirectory()); 
  //If exist then check for Path.Combine()
}

Note : Current directory is distinct from the original directory, which is the one from which the process started

Most of the time it is directory of your bin folder

You can set CurrentDirectory by passing parameter to your function

Directory.SetCurrentDirectory(@"c:\program files\");

Check Directory as earlier answer should tell if the directory exists. Don't know if you've copy and pasted the file name in your question. In that case, you've spelled the filename wrong since it's spelled with 3 x T where you mention the filename and 2 x T in your code. Could be easy to miss, think everyone has done that once or "twice".

Try to take the path as strings to check that the code can find your file.

if (File.Exists(Path.Combine(filedirectoryPathAsString, fileName))

This answer is for the Edited question. You can change the output directory by Right click on project -> Properties -> Build -> Output -> Output path

Also look into this solution to see the difference: https://stackoverflow.com/a/27322567/4731319

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM