If I have an executable called app.exe which is what I am coding in C#, how would I get files from a folder loaded in the same directory as the app.exe, using relative paths?
This throws an illegal characters in path exception:
string [ ] files = Directory.GetFiles ( "\\Archive\\*.zip" );
How would one do this in C#?
To make sure you have the application’s path (and not just the current directory), use this:
Now you have a
Processobject that represents the process that is running.Then use
Process.MainModule.FileName:Finally, use
Path.GetDirectoryNameto get the folder containing the .exe:So this is what you want:
(Notice that
"\Archive\"from your question is now@"\Archive\": you need the @ so that the\backslashes aren’t interpreted as the start of an escape sequence)Hope that helps!