我想在Resources目錄中打開一個包含的HTML文件,但它看起來像我的路徑錯誤或者我犯了一些其他錯誤。在目錄中打開一個文件Resources
我目前在一個表單類,我想要打開文件,如果用戶按下按鈕F1。
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "f1.html";
proc.Start();
我想在Resources目錄中打開一個包含的HTML文件,但它看起來像我的路徑錯誤或者我犯了一些其他錯誤。在目錄中打開一個文件Resources
我目前在一個表單類,我想要打開文件,如果用戶按下按鈕F1。
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "f1.html";
proc.Start();
如果你想獲得一些嵌入式的資源,你需要調用
ResourceManager.GetStream
http://msdn.microsoft.com/en-us/library/zxee5096.aspx
它會返回一個內存流。讀存儲器流以一個字節數組和字節數組寫入到一些臨時位置,然後使用臨時文件作爲參數的路徑調用
Process.Start()
。
這裏是一個示例代碼:
public class Class1{
public static void Main(string[] args){
FileStream stream = null;
string fullTempPath = null;
try{
byte[] page = Resources.HTMLPage1;
fullTempPath = Path.GetTempPath() + Guid.NewGuid() + ".html";
stream = new FileStream(fullTempPath, FileMode.Create, FileAccess.Write, FileShare.Read);
stream.Write(page, 0, page.Length);
stream.Flush(true);
stream.Close();
Process proc = new Process{StartInfo ={FileName = fullTempPath}};
proc.Start();
}
finally{
if (stream != null){
stream.Dispose();
}
}
}
}
使用此代碼,打開HTML文件使用的是默認的瀏覽器
string filename = Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "f1.html";
System.Diagnostics.Process.Start(filename);
這不起作用。 – Proton
你是什麼意思與「資源目錄」?應用程序資源或一些物理目錄在你的硬盤上? – Oscar
什麼是錯誤?它在運行時還是編譯時間?你刪除了括號嗎? –
我的意思是應用程序資源。該錯誤是文件未找到,是的,我刪除了它們。 – Proton