Solved

File.Exists() is not working in asp.net C#

Posted on 2010-09-21
4
860 Views
Last Modified: 2013-12-16
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
Comment
Question by:njgroup
  • 2
4 Comments
 
LVL 23

Expert Comment

by:adilkhan
ID: 33730038
Try ...
string file_path = @"~/emails/" + emailFileName;

also make sure file_path actualy has an valid value in it.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 33730142
"~/emails" won't resolve by itself. Try:


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

Open in new window

0
 

Author Closing Comment

by:njgroup
ID: 33735975
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33736064
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now