Reputation: 65
I've found a few related questions but they're not working that well. The image name is a modified GUID like 3c6b4a9b-8e88-4c8e-93da-258acd2c964f_0
but the extension isn't known (.jpg, .gif, ..etc). The GUID will be coming from a gridview so it's not a static string. Below is what I have but I'm having a difficult time getting the path to work correctly.
string fileName = `3c6b4a9b-8e88-4c8e-93da-258acd2c964f`;
DirectoryInfo filePath = new DirectoryInfo(@"/Images");
MessageBox.Show(filePath.ToString());
FileInfo[] fileArray = filePath.GetFiles(fileName + "_0.*");
Keep getting issues with the directory being invalid. Currently the files are stored on my c: drive.
How can I get the relative path without hardcoding it in? I was using DirectoryInfo(Server.MapPath("Images"));
which worked temporarily then started giving this error This doesn't seem to be a permanent solution once the site is launched though.System.ArgumentException: Second path fragment must not be a drive or UNC name.
which seems to be from the path having the drive "C:"
The actual path is C:\Website\Name\Images\3c6b4a9b-8e88-4c8e-93da-258acd2c964f_0.jpg
Thanks!
Upvotes: 0
Views: 599
Reputation: 1740
The problem is that you are getting DirectoryInfo
for "C:\Images".
You want to use Server.MapPath
to get the physical path to the folder that is in your website (which could be anywhere on any drive).
Using the ~
means to start from the root of the running website.
So this should do the trick:
string fileName = `3c6b4a9b-8e88-4c8e-93da-258acd2c964f`;
DirectoryInfo filePath = new DirectoryInfo(Server.MapPath("~/Images"));
FileInfo[] fileArray = filePath.GetFiles(fileName + "_0.*");
Upvotes: 1
Reputation: 383
You've used filePath
as the first parameter to GetFiles
, just use the wildcard and invoke the overload of GetFiles
with one parameter.
filePath.GetFiles("_0.*");
Upvotes: 1