How to get files in a relative path in C#
Solution 1
To make sure you have the application's path (and not just the current directory), use this:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getcurrentprocess.aspx
Now you have a Process
object that represents the process that is running.
Then use Process.MainModule.FileName
:
http://msdn.microsoft.com/en-us/library/system.diagnostics.processmodule.filename.aspx
Finally, use Path.GetDirectoryName
to get the folder containing the .exe:
http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx
So this is what you want:
string folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Archive\";
string filter = "*.zip";
string[] files = Directory.GetFiles(folder, filter);
(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!
Solution 2
string currentDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string archiveFolder = Path.Combine(currentDirectory, "archive");
string[] files = Directory.GetFiles(archiveFolder, "*.zip");
The first parameter is the path. The second is the search pattern you want to use.
Solution 3
Write it like this:
string[] files = Directory.GetFiles(@".\Archive", "*.zip");
. is for relative to the folder where you started your exe, and @ to allow \ in the name.
When using filters, you pass it as a second parameter. You can also add a third parameter to specify if you want to search recursively for the pattern.
In order to get the folder where your .exe actually resides, use:
var executingPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
Solution 4
As others have said, you can/should prepend the string with @
(though you could also just escape the backslashes), but what they glossed over (that is, didn't bring it up despite making a change related to it) was the fact that, as I recently discovered, using \
at the beginning of a pathname, without .
to represent the current directory, refers to the root of the current directory tree.
C:\foo\bar>cd \
C:\>
versus
C:\foo\bar>cd .\
C:\foo\bar>
(Using .
by itself has the same effect as using .\
by itself, from my experience. I don't know if there are any specific cases where they somehow would not mean the same thing.)
You could also just leave off the leading .\
, if you want.
C:\foo>cd bar
C:\foo\bar>
In fact, if you really wanted to, you don't even need to use backslashes. Forwardslashes work perfectly well! (Though a single /
doesn't alias to the current drive root as \
does.)
C:\>cd foo/bar
C:\foo\bar>
You could even alternate them.
C:\>cd foo/bar\baz
C:\foo\bar\baz>
...I've really gone off-topic here, though, so feel free to ignore all this if you aren't interested.
Solution 5
Pretty straight forward, use relative path
string[] offerFiles = Directory.GetFiles(Server.MapPath("~/offers"), "*.csv");
Comments
-
Joan Venge about 4 years
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#?
-
Kieren Johnstone almost 14 years.\ means relative to the current directory, I think?
-
Eric Olsson almost 14 yearsA single dot (.) can stand in for the current directory. Two dots (..) mean the parent of the current directory.
-
Kieren Johnstone almost 14 yearsExactly, but the question is talking about relative to the executable, not the current directory..?
-
Mikael Svenson almost 14 yearsUpdated my answer to specify the ., and where your exe resides.
-
Mikael Svenson almost 14 yearsPath.GetDirectoryName(Assembly.GetEntryAssembly().Location); is simpler imo.
-
Moe Sisko almost 14 yearsAdding to Kieren's comment : there's also things like OpenFileDialog and SaveFileDialog which can change the current directory at runtime. (if RestoreDirectory property is left at its default value of false).
-
Adam Lear almost 14 yearsFair point. I updated my answer. @Moe: Good to know. I didn't know RestoreDirectory existed.
-
Piotr Kula over 11 yearsHow do you do it for a virtual directory that is in the top root of the web application that is mapped to UNC path?
-
yoyo almost 11 yearsSlightly off topic: note that if you want files in the current directory you cannot pass an empty string as the path argument to Directory.GetFiles, but "." works.