Solved

File.Exists() path problem

Posted on 2013-01-17
6
385 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

751 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