Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 883
  • Last Modified:

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!!
0
njgroup
Asked:
njgroup
  • 2
1 Solution
 
Saqib KhanSenior DeveloperCommented:
Try ...
string file_path = @"~/emails/" + emailFileName;

also make sure file_path actualy has an valid value in it.
0
 
Carl TawnSystems and Integration DeveloperCommented:
"~/emails" won't resolve by itself. Try:


string file_path = Path.Combine(Server.MapPath("~/emails"), emailFileName);

Open in new window

0
 
njgroupAuthor Commented:
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?
0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now