2015-06-05 34 views
2

目前我做了以下內容:運行exe文件,同時它仍然打開C#

  1. 包含在資源文件中的EXE文件。
  2. 在運行時,我提取EXE文件並將其寫入HDD上的文件,並使用「DeleteOnClose」選項。
  3. 很明顯,II不關閉文件,所以它仍然在磁盤上。
  4. 我去路徑並雙擊它,但它拒絕打開。
  5. 如果我使用「Process.Start」,我得到文件不存在的錯誤,雖然它在給定的路徑中。
  6. 我將FileShare和FileAccess設置爲ReadWrite。
  7. 再次Process.Start不起作用。

這裏是我使用的代碼:

byte[] exeFile =ExeSecure.Properties.Resources.ReqCheck; 
//2) Create file to be deleted on close 
FileStream aFile = new FileStream(@"c:\reco.exe", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 20000, FileOptions.DeleteOnClose); 
//3) Write Exe file content to HDD 
aFile.Write(exeFile, 0, exeFile.Length); 
aFile.Flush(); 
Thread.Sleep(100); //Wait a while for file to be flushed 
while (!File.Exists(@"c:\reco.exe")); //Make sure file is there on HDD 
    Process.Start("C:\reco.exe");//Start file , this always fails. 
+1

沒有關係,但我會一直增加一些延遲while循環 –

回答

2

或許這只是在你的問題一個錯字,但在Process.Start路徑不正確。你缺少@標誌:

Process.Start(@"C:\reco.exe"); 
+0

我第一次聽到'FileOptions.DeleteOnClose'。我沒有看到它的任何用法......(我錯了嗎?)?它基本上意味着該文件必須調用.flush才能使其成爲整個文件,然後在不關閉文件的情況下使用它。這不是很奇怪嗎? –

+0

只要文件是從C#代碼打開的,你永遠不能在Windows中雙擊它並讓它運行,也不能使用Process.Start()來執行它,並且刪除文件的選項關閉是非常有用的,如果你從你的資源啓動一些exe文件,並希望在完成它們之後刪除它們,所以你可以通過這個選項指示Windows這麼做。 –

相關問題