简体   繁体   中英

Why Can't my application open a file that exists?

I'm currently building a Web Service that sends files this also obtains the mime-type from the file is sending but for some reason my application can't see the file let alone open.

My path is

file:\\C:\\Users\\Martin\\Documents\\Visual Studio 2010\\Projects\\HTML5Streamer\\
Debug\\Player\\player.html

(this is created by my application. My application is compiled to the debug folder in that path when I use Chrome locally and paste that address it works fine, chrome can see and access the file,

My VS is running as Administrator so the application compile is also running as Admin why would it get the correct path then the File.Exists() says it does not exist

 public string getMimeFromFile(string filename)
    {
        if (!File.Exists(filename))
            throw new FileNotFoundException(filename + " not found"); // this is thrown

        byte[] buffer = new byte[256];
        using (FileStream fs = new FileStream(filename, FileMode.Open))
        {
            if (fs.Length >= 256)
                fs.Read(buffer, 0, 256);
            else
                fs.Read(buffer, 0, (int)fs.Length);
        }
        try
        {
            System.UInt32 mimetype;
            FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
            System.IntPtr mimeTypePtr = new IntPtr(mimetype);
            string mime = Marshal.PtrToStringUni(mimeTypePtr);
            Marshal.FreeCoTaskMem(mimeTypePtr);
            return mime;
        }
        catch (Exception e)
        {
            return "unknown/unknown";
        }
    }

The method calling this is

private void ProccessPlayerRequest(HttpListenerContext context)
        {
            Uri content_uri = context.Request.Url;
            string filePath = ApplicationPath + content_uri.AbsolutePath.Replace('/', '\\');
//ApplicationPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            string mimeType = getMimeFromFile(filePath);

            context.Response.Headers.Add("Content-Type: " + mimeType);
            streamFileToBrowser(filePath, context);
        }

When using string filePath = Path.Combine(ApplicationPath, @"Player\\player.html"); produced

"file:\\C:\\Users\\Martin\\Documents\\Visual Studio 2010\\Projects\\HTML5Streamer\\Debug\\Player\\player.html"

and yes file:\\ is got from the .Net Framework with

string ApplicationPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            Service serv = new Service(ApplicationPath);

This application path sent into the constructor is what is given from all the correct calls so please dont say it should not have file:\\ as that is given by the .Net Framework

Try removing the file:\\\\ from the start of the filename.

Edit: Try using Application.ExecutablePath instead of the CodeBase reference in your Path.GetDirectoryName() call. You may have to add a reference to the assembly System.Windows.Forms .

string ApplicationPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

Edit2:

You may also be able to use (taken from a dll of mine):

public static FileInfo Application()
{
    return new FileInfo(Environment.GetCommandLineArgs()[0]);
}

Then:

string ApplicationPath = Application().DirectoryName;

System.IO.File works with file names, not URIs, so the file:/ prefix won't be recognized. Chrome works because it's converting a URI to a system file name for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM