• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1132
  • Last Modified:

How can I check if file "file://localhost/C:My%20Song.mp3" exists?

How can I check if this file exists?

file://localhost/C:/Users/Public/My%20Music/MySong.mp3

I tried getting rid of the %20 using UnescapeDataString, which gave me:

file://localhost/C:/My%20Song.mp3

But I still have that leading "file://localhost/" and I suppose this file might be something else too.

string newval = System.Uri.UnescapeDataString(value);
FileInfo info = new FileInfo(newval);  //Crashes. Doesn't like URI format.

Open in new window

0
deleyd
Asked:
deleyd
  • 2
4 Solutions
 
jeebukarthikeyanCommented:
HI,

It seems the url seems to be wrong

file://localhost/C:/Users/Public/My%20Music/MySong.mp3

create a virtual directory and put the mp3 on that directory

so that the url will be sme thing like file://localhost/My20Music/MySong.mp3

string filePath =url
  
        FileInfo sng= new FileInfo(filePath);  
        bool fileExists = sng.Exists;  
        Label1.Text = "File exits?: " + fileExists.ToString();  

Open in new window


jeebu
0
 
saraganiCommented:
           Uri uri = new Uri(@"file://localhost/C:/Users/Public/My%20Music/MySong.mp3");
            string filePath = uri.AbsolutePath;

            // To also want to support "file://C:/Users/Public/My%20Music/MySong.mp3"
            if (filePath.StartsWith("/"))
            {
                filePath = filePath.Remove(0, 1);
            }
            FileInfo info = new FileInfo(filePath);
0
 
saraganiCommented:
Furthermore, if you just want to check if the file exists then don't use FileInfo.

Just use:
System.IO.File.Exists(filePath);  
0
 
cubaman_24Commented:
Hello:
Use HttpServerUtility.HtmlDecode() method to un-escape the file name, which is html encoded. Then, use File.Exists as pointed before. Be aware that AspNet process identity normally doesn't have access to folders outside application folder, and explicit permission should be given.
Best regards.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now