Solved

File.Exists() path problem

Posted on 2013-01-17
6
369 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DateTimepicker 4 33
Run time Error 4 35
free scanner TWAIN can be operated with a Web application 9 41
Capture logon name 13 48
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

861 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

23 Experts available now in Live!

Get 1:1 Help Now