njgroup
asked on
File.Exists() is not working in asp.net C#
hi, I am looking for a file in web directory "emails" and I wanna check if file is exists already or not, but that was not working...
here is code:
string contactID = idGenerate(10);
string emailFileName = name + "_" + contactID + ".html";
string file_path = "~/emails/" + emailFileName;
while (File.Exists(file_path))
{
contactID = idGenerate(10);
emailFileName = name + "_" + contactID + ".html";
}
the File.Exists(file_path) always return false even if file already there!!
here is code:
string contactID = idGenerate(10);
string emailFileName = name + "_" + contactID + ".html";
string file_path = "~/emails/" + emailFileName;
while (File.Exists(file_path))
{
contactID = idGenerate(10);
emailFileName = name + "_" + contactID + ".html";
}
the File.Exists(file_path) always return false even if file already there!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes, this one works fine, but let me ask you please, why it's not working in normal way as:
string file_path = "~/emails/" + emailFileName;
however, its working if I put it as:
linkedButton1.Image = "!/images/img.png";
so, what is the difference?
string file_path = "~/emails/" + emailFileName;
however, its working if I put it as:
linkedButton1.Image = "!/images/img.png";
so, what is the difference?
Because in the first example you are assigning the literal value "~/emails" to a sting variable. he second example is assigning to a control property. The URL in the control property will be converted to it's correct virtual path automatically when the control is rendered.
string file_path = @"~/emails/" + emailFileName;
also make sure file_path actualy has an valid value in it.