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#?
Asked
Active
Viewed 678 times
-4
-
1What is the tag spam for? This is a C# question. There is no need mention any of the other tags. – Stephen C Nov 27 '16 at 08:05
-
can you mention the directory structure? – Dharminder Singh Nov 27 '16 at 08:07
-
Path.Combine(Application.StartupPath , "data"); – Jeremy Thompson Nov 27 '16 at 08:08
2 Answers
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