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
The only way to get smarter is by playing a smarter opponent.
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.
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...
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.
The only way to get smarter is by playing a smarter opponent.
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.
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 page
Directory.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?
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.
Member
281 Points
549 Posts
Directory.Exists(path) returns false!
Jun 29, 2020 01:14 PM|N1ZAM|LINK
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.
Any suggestions?
Regards
NIZAM
All-Star
48510 Points
18072 Posts
Re: Directory.Exists(path) returns false!
Jun 29, 2020 01:24 PM|PatriceSc|LINK
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.
Participant
1061 Points
665 Posts
Re: Directory.Exists(path) returns false!
Jun 29, 2020 01:35 PM|jzero|LINK
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
Member
281 Points
549 Posts
Re: Directory.Exists(path) returns false!
Jul 03, 2020 03:40 PM|N1ZAM|LINK
None of the above worked.
NIZAM
All-Star
48510 Points
18072 Posts
Re: Directory.Exists(path) returns false!
Jul 03, 2020 05:57 PM|PatriceSc|LINK
Could you be explicit about the value you have in path ? If a real path you should later use :
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...
Member
281 Points
549 Posts
Re: Directory.Exists(path) returns false!
Jul 05, 2020 06:59 PM|N1ZAM|LINK
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
This is always showing null and default image gets displayed. Any reason for this.
NIZAM
All-Star
53041 Points
23612 Posts
Re: Directory.Exists(path) returns false!
Jul 05, 2020 07:12 PM|mgebhard|LINK
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.
Contributor
5961 Points
2466 Posts
Re: Directory.Exists(path) returns false!
Jul 05, 2020 09:29 PM|KathyW|LINK
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 page
Directory.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?
All-Star
48510 Points
18072 Posts
Re: Directory.Exists(path) returns false!
Jul 06, 2020 07:28 AM|PatriceSc|LINK
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 :
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.
Member
281 Points
549 Posts
Re: Directory.Exists(path) returns false!
Jul 07, 2020 04:52 AM|N1ZAM|LINK
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)); } }
NIZAM