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.ExecutablePathinstead of the CodeBase reference in yourPath.GetDirectoryName()call. You may have to add a reference to the assemblySystem.Windows.Forms.Edit2:
You may also be able to use (taken from a dll of mine):
Then: