Link to home
Start Free TrialLog in
Avatar of njgroup
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!!
Avatar of Saqib Khan
Saqib Khan
Flag of United States of America image

Try ...
string file_path = @"~/emails/" + emailFileName;

also make sure file_path actualy has an valid value in it.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of njgroup
njgroup

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?
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.