2014-03-27 31 views
0

我想在Resources目錄中打開一個包含的HTML文件,但它看起來像我的路徑錯誤或者我犯了一些其他錯誤。在目錄中打開一個文件Resources

我目前在一個表單類,我想要打開文件,如果用戶按下按鈕F1。

System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
proc.StartInfo.FileName = "f1.html"; 
proc.Start(); 
+0

你是什麼意思與「資源目錄」?應用程序資源或一些物理目錄在你的硬盤上? – Oscar

+0

什麼是錯誤?它在運行時還是編譯時間?你刪除了括號嗎? –

+0

我的意思是應用程序資源。該錯誤是文件未找到,是的,我刪除了它們。 – Proton

回答

1

如果你想獲得一些嵌入式的資源,你需要調用

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(); 
      } 
     } 
    } 
} 
+0

爲什麼打開文件非常困難..爲什麼我必須將它寫入臨時位置。你能給我一個完整的例子嗎? – Proton

+0

@ user3438236這並不那麼困難,只是一些步驟而已。我用示例代碼更新了我的答案。我已經使用名爲HTMLPage1.htm的二進制格式向該項目添加了一個資源,以使該示例正常工作。我建議在臨時寫作,以確保你有寫在目錄中的權利。 – Oscar

+0

這對我不起作用:byte [] page = Resources.HTMLPage1;好像我不能使用資源。 – Proton

0

使用此代碼,打開HTML文件使用的是默認的瀏覽器

 string filename = Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "f1.html"; 

     System.Diagnostics.Process.Start(filename); 
+0

這不起作用。 – Proton

相關問題