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

Posted on 2011-05-03
Last Modified: 2012-05-11
How can I check if this file exists?


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


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

Question by:deleyd
    LVL 13

    Accepted Solution


    It seems the url seems to be wrong


    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

    LVL 11

    Assisted Solution

               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);
    LVL 11

    Assisted Solution

    Furthermore, if you just want to check if the file exists then don't use FileInfo.

    Just use:
    LVL 8

    Assisted Solution

    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.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    745 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

    15 Experts available now in Live!

    Get 1:1 Help Now