Solved

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

Posted on 2010-09-21
4
875 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Saqib Khan
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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