Solved

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

Posted on 2010-09-21
4
855 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

11 Experts available now in Live!

Get 1:1 Help Now