Answered by:
Directory.Exists(path) returns false!

Question
-
User739135361 posted
Hi,
I am working on a application were in I am trying to save a profile image of a user while creating a profile. While I am trying to retreive it, I always get
Directory.Exists(path) as false. and Else block is getting executed every time which returns a default image. I thought it could be due to permission of the folder, but even after removing read only attribute, I am still getting the same issue.
Note that the folder is getting created, image is getting saved. Issue is while retreiving the image to display. Below is the code for retreiving the image and displaying it.
if (Directory.Exists(path)) { var files = Directory.EnumerateFiles(Server.MapPath(path), "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".png") || s.EndsWith(".jpg") || s.EndsWith(".jpeg") || s.EndsWith(".bmp") || s.EndsWith(".gif")).ToList(); for (int i = 0; i < files.Count; i++) { mp.ProfilePics = new List<string>(); mp.ProfilePics.Add(path + "\\" + files[0].Substring(files[i].LastIndexOf("\\") + 1)); } } else { mp.ProfilePics = new List<string>(); string str = mp.Gender.GenderID == 1 ? ConfigurationManager.AppSettings["Male"].ToString() : ConfigurationManager.AppSettings["FeMale"].ToString(); mp.ProfilePics.Add(str); }
Any suggestions?
Regards
Monday, June 29, 2020 1:14 PM
Answers
-
User409696431 posted
What is the value of "path"? It needs to be a virtual path in the Web application if you are using Server.MapPath, such as:
Server.MapPath("/Images/Folder1"); if the Images folder is right under the root of the application
or
Server.MapPath("../Images/Folder1); if the Images folder is one level above the current pageDirectory.Exists(path) takes a relative or absolute path. An absolute path includes the drive letter, and is not the same as a virtual path you'd use in Server.MapPath.
Are you using a path variable that can't be used for both functions?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, July 5, 2020 9:29 PM -
User753101303 posted
Despite being asked multiple times, you never told yet which value you have in path. I suspect this is a virtual path (ie "/folder") rather than a physical path and if confrmed :
path=Server.MapPath(path); // turns "/folder" to "c:\sites\mysite\folder" or whatever if (Directory.Exists(path)) ...
could fix the issue. If it is already a physical path you don't use a mapped drive letter?
If still doesn't work rather than keep wondering what happens I would llook at what happens possibly ddoing small testing changes such as trying to create a file ion this folder to see if it created in the correct folder or if I have some error that should help finding out what happens.
For now I keep thinking the path is bad until proven otherwise.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 6, 2020 7:28 AM
All replies
-
User753101303 posted
Hi,
Start by checking which value you have in your path variable. As you are using Server.MapPath(path), it seems it could be virtual path rather than a physical path.
If you have a value such as "/site/folder" you'll end up in testing "<current drive>:\site\folder" rather than "z:\inetpub\site\folder" or whatever is the real location for this virtual path.
Monday, June 29, 2020 1:24 PM -
User-943250815 posted
Is there at least "read-only" permission on directory?
https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.exists?view=netcore-3.1
Monday, June 29, 2020 1:35 PM -
User739135361 posted
None of the above worked.
Friday, July 3, 2020 3:40 PM -
User753101303 posted
Could you be explicit about the value you have in path ? If a real path you should later use :
Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories) // rather than Server.MapPath(path)
If unsure about what happens I'm doing quick experiments to see what happens for example here I would likely try to create a test file in this folder to see which kind of error I have which should help to understand if the folder path is wrong, if this is permission issue or whatever else...
Friday, July 3, 2020 5:57 PM -
User739135361 posted
I am trying to create profiles of users. Their uploaded images are stored in dynamically created folders. While accessing their profiles, I am trying to check if there is a valid path and if the file exists
thats where i am checking
if (Directory.Exists(path))
This is always showing null and default image gets displayed. Any reason for this.
Sunday, July 5, 2020 6:59 PM -
User475983607 posted
Clearly, the file or directory does not exist. Use the Visual Studio debugger to get the path variable and share the path on the forum. Also share the code that saves the file on the server.
Sunday, July 5, 2020 7:12 PM -
User409696431 posted
What is the value of "path"? It needs to be a virtual path in the Web application if you are using Server.MapPath, such as:
Server.MapPath("/Images/Folder1"); if the Images folder is right under the root of the application
or
Server.MapPath("../Images/Folder1); if the Images folder is one level above the current pageDirectory.Exists(path) takes a relative or absolute path. An absolute path includes the drive letter, and is not the same as a virtual path you'd use in Server.MapPath.
Are you using a path variable that can't be used for both functions?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, July 5, 2020 9:29 PM -
User753101303 posted
Despite being asked multiple times, you never told yet which value you have in path. I suspect this is a virtual path (ie "/folder") rather than a physical path and if confrmed :
path=Server.MapPath(path); // turns "/folder" to "c:\sites\mysite\folder" or whatever if (Directory.Exists(path)) ...
could fix the issue. If it is already a physical path you don't use a mapped drive letter?
If still doesn't work rather than keep wondering what happens I would llook at what happens possibly ddoing small testing changes such as trying to create a file ion this folder to see if it created in the correct folder or if I have some error that should help finding out what happens.
For now I keep thinking the path is bad until proven otherwise.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 6, 2020 7:28 AM -
User739135361 posted
Hi,
as you all rightly said, I was checking Directory.Exists(path)
This one works
if (Directory.Exists(Server.MapPath(path))) { var files = Directory.EnumerateFiles(Server.MapPath(path), "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".png")).ToList(); for (int i = 0; i < files.Count; i++) { mp.ProfilePics = new List<string>(); mp.ProfilePics.Add(path + "\\" + files[0].Substring(files[i].LastIndexOf("\\") + 1)); } }
Tuesday, July 7, 2020 4:52 AM