Solved

File.Exists() path problem

Posted on 2013-01-17
6
379 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 63

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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