-4

I have a folder with my exe and a folder called data. How can I access the data folder relative to my exe that is currently open in the same folder as the data folder in C#?

Denis Wasilew
  • 503
  • 3
  • 9
  • 29
Sparsite2
  • 1
  • 4

2 Answers2

0

sample folder structure:

--ApplicationFolder
       |------> your exe
       |------> Data folder
                 |------> files inside data folder 

get full paths from relative path

string applicationFolderPath =Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

string dataFolderPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Data");
Damith
  • 62,401
  • 13
  • 102
  • 153
0

Suppose you have this heirarchy:

/*  bin
       Debug
          Data
              file.txt
          App.exe
*/

If you're running App.exe and you want to access file.txt, you can easily do that using:

string str = System.IO.File.ReadAllText("data\\file.txt");

If your goal is to jump up in the heirarchy:

/*  bin
      DataBefore
           file2.txt
      Debug
           App.exe
*/

Then you can use:

string str = System.IO.File.ReadAllText("..\\DataBefore\\file2.txt");
Zein Makki
  • 29,485
  • 6
  • 52
  • 63