0

I have an XML file in outside of my project folder, and I want to access it from my code, in order for the code to be executed on whatever machine I would put the path relative to the project's directory.

Lets say as an example that my current directory is in Folder A, and the file I want to access is in Folder B next to A.

eandersson
  • 25,781
  • 8
  • 89
  • 110
Marwan Tushyeh
  • 1,505
  • 4
  • 24
  • 47
  • 3
    http://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path – kenny Feb 10 '13 at 11:10
  • 1
    The project's directory (as in, the directory of the `.csproj` file) will be irrelevant on the target machine, but the directory of the executable file (the `.exe` file) may be. What *is* the relative path between the executable file and the Xml file? – O. R. Mapper Feb 10 '13 at 11:10
  • Do you have any control on the location of this file? If it is generated from some other application or user action there is always the chance that something changes. Why not use a configuration file and read the exact location there? – Steve Feb 10 '13 at 12:21

1 Answers1

4

If the XML file is always inside your application folder you can use.

Environment.CurrentDirectory

The working path may not necessarily be where the executable file is located. To be sure you can use the following code taken from MSDN.

string path;
path = System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );

Otherwise, if it is part of Microsoft's special folders, like .e.g MyDocuments you can use.

Environment.SpecialFolder.MyDocuments

You would use it like this.

Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                           "myfile.xml")

The output would be the path to the myfile.xml in the current users My Documents folder. In my case it would give me C:\Users\eandersson\Documents\myfile.xml.

Path.Combine is very helpful here as it will allow us to combine multiple disk paths into one.

Edit: Additional information requested by comment.

I think the best approach would be to use Directory.GetParent.

Directory.GetParent(Environment.CurrentDirectory).FullName

And do something like this.

Path.Combine(Directory.GetParent(Environment.CurrentDirectory).FullName, "PathB", "myfile.xml")

This would look for PathB in the same location as your Project Folder.

C:\MyProjects\PathA\MyExecutable.exe
C:\MyProjects\PathB\myfile.xml

Lets say that you are running MyExecutable.exe from that location. The above code should automatically return the second location inside PathB with the file myfile.xml.

eandersson
  • 25,781
  • 8
  • 89
  • 110
  • lets say my current directory is in Folder A, and the file is in Folder B next to A..how can this solve it? – Marwan Tushyeh Feb 10 '13 at 11:17
  • Please, let me know if that was what you were looking for. :) – eandersson Feb 10 '13 at 11:31
  • "If the XML file is always inside your application folder you can use. `Environment.CurrentDirectory`" - wrong. The current working directory doesn't have to be the same as the directory where the application is stored. – O. R. Mapper Feb 10 '13 at 11:54
  • 1
    I updated the answer @O.R.Mapper, while it may be a trap, it's only in special scenarios that the executable and working directory will be different. – eandersson Feb 10 '13 at 12:17