Solved

File.Exists() path problem

Posted on 2013-01-17
6
365 Views
Last Modified: 2013-04-03
I'm trying to check for the existence of files in a virtual folder called Audio, which resides in the root of my .NET app.

Is it required that I enter the literal path for File.Exists()?  Because this simple test is not finding the file:
if (File.Exists("test.mp3"))

Yet, this DOES find the file:
if (File.Exists("C:\\Inetpub\\OAS2\\OAS2\\MusicTeam\\test.mp3"))

What I want to do is point to the Audio folder on the root of my app. But this did not work:
if (File.Exists(~/Audio/test.mp3))
0
Comment
Question by:Xbradders
6 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 38789041
Hi Xbradders;

From Microsoft documentation [HERE]:
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, use GetCurrentDirectory() method.
0
 
LVL 21

Assisted Solution

by:masterpass
masterpass earned 93 total points
ID: 38789223
use Server.MapPath

read here : http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx

It should be something like

if (File.Exists(Server.MapPath("~/Audio/test.mp3")))

So it will look into the Audio folder in the root of your application for test.mp3 file.

Hope this helps.
0
 
LVL 19

Assisted Solution

by:Amandeep Singh Bhullar
Amandeep Singh Bhullar earned 93 total points
ID: 38792526
hi,

You can get the application root path with
string APP_PATH = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();

then applent your path a

APP_PATH += "Audio/test.mp3"

Hope this helps...
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Accepted Solution

by:
Monica P earned 94 total points
ID: 38800055
Get The base location and add your defined folder name and file name with it

 Dim base As String = AppDomain.CurrentDomain.BaseDirectory

 Dim Filename as string="test.mp3"

   base = base + Filename   ---> (contain entire filename )

or use

To get base Path of running application ---> use "Application.StartupPath "

and then check existence as below

If System.IO.File.Exists(base ) Then
--Your code here
End If
0
 

Author Comment

by:Xbradders
ID: 38828582
Sorry for the delay in responding - I got sidetracked with putting out fires.

Thanks for the responses, but I don't think these solutions work with virtual paths, which I am trying to access here.  

The Audio folder is a virtual directory that points to a folder on another server that contains the files I'm checking the existence of.  Is there a special way to refer to files in a virtual folder?  This does not find the file:

File.Exists(Audio/ULTIMATE CLASSIX/XCL001 BEETHOVEN VOL 1/FULL LENGTH/XCL001_06 Egmont - Overture.mp3)

BUT, When I past the path into my browser, it does play the audio, so it is there.  Like so"
http://www.mysite.com/Audio/ULTIMATE CLASSIX/XCL001 BEETHOVEN VOL 1/FULL LENGTH/XCL001_06 Egmont - Overture.mp3

Any idea what I'm doing wrong in code?
0
 

Author Closing Comment

by:Xbradders
ID: 39045899
I didn't find the solution and did something else.  thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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:…
This video discusses moving either the default database or any database to a new volume.
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.

744 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

10 Experts available now in Live!

Get 1:1 Help Now