Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

File.Exists() path problem

Posted on 2013-01-17
6
Medium Priority
?
424 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 64

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 186 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 186 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 10

Accepted Solution

by:
Monica P earned 188 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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.
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
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

824 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