2014-02-19 32 views
0

我有一個關於在VB .NET項目中運行exe文件的問題。從VB.NET項目運行VB類型的exe文件

我以前讀的文章 http://www.dreamincode.net/forums/topic/244460-how-to-run-a-exe-file-from-inside-a-vs2010-project/

運行從VB.NET項目內的exe文件。

我用

的Process.Start( 「My.Resources \ MyProgram.exe」) 和 System.Diagnostics.Process.Start(My.Computer.FileSystem.SpecialDirectories.Desktop &「\屏幕

然而.EXE「)

運行exe文件,它沒有任何發生。因此,我認爲這條道路是錯誤的運行exe文件

這裏是我做了運行exe文件

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

If My.Computer.FileSystem.FileExists((My.Computer.FileSystem.SpecialDirectories.Desktop & "\screen.exe")) Then 
    MsgBox("yes") 
     Process.Start((My.Computer.FileSystem.SpecialDirectories.Desktop & "\screen.exe")) 
    Else 
     MsgBox("np") 
    End If 

我手動點擊exe文件,它正常運行。 此外,我使用if語句來確定是否存在文件

它返回true表示存在一個exe文件。

但是,當我在Visual Studio 2012(VB.NET)項目中運行。 沒有錯誤,它似乎沒有運行文件。

,我真的不知道爲什麼發生這種情況

有誰知道爲什麼出現這種情況?謝謝

+1

什麼是調用Process.S的返回值酸? –

+0

我的示例exe文件應該截取屏幕截圖。當我雙擊exe文件。它正在工作。但是,當我運行我的VB.NET項目來運行exe文件。只是沒有發生... –

+0

我不相信exe文件返回它。 exe文件只是截圖 –

回答

1

我已經測試過這個,它在我的本地電腦上運行。 確保你有正確的exe文件路徑。

Process.Start("FolderPath\MyProgram.exe") 

請檢查本網站爲此下載代碼示例。 Example Code

0

首先,資源不是文件,因此您無法使用Process.Start執行它們。這將只執行一個文件,所以你需要提取你的資源,這只是你自己的程序文件中的字節,並將其另存爲一個單獨的文件。這在Windows上是一個安全問題,所以甚至可能不被允許。

至於實際EXE運行不正常,一個可能的原因是無效的工作目錄。嘗試創建一個ProcessStartInfo對象並將其WorkingDirectory屬性設置爲包含您正在運行的EXE的文件夾。這可以解決在其工作目錄中查找依賴關係的程序的問題。

0

對於我來說,如果執行資源或執行現有的可執行文件,我真的不清楚你真正需要什麼。

要啓動的EXE,它是在你的資源,你可以將其加載到磁盤上是這樣的:

' Load Resource To Disk 
' (By Elektro) 
' 
' Usage Examples: 
' LoadResourceToDisk(My.Resources.Program, "C:\Program.exe") 
' Process.Start("C:\Program.exe") 
' 
''' <summary> 
''' Loads a resource to disk. 
''' </summary> 
''' <param name="Resource"> 
''' Indicates the resource to load. 
''' </param> 
''' <param name="TargetFile"> 
''' Indicates the target file to save the resource. 
''' The target file is overwritten. 
''' </param> 
''' <returns> 
''' <c>true</c> if operation succeeds, <c>false</c> otherwise. 
''' </returns> 
''' <exception cref="Exception"></exception> 
Private Function LoadResourceToDisk(ByVal Resource As Byte(), 
            ByVal TargetFile As String) As Boolean 

    Try 
     Dim BufferFileStream As New IO.FileStream(TargetFile, IO.FileMode.Create, IO.FileAccess.Write) 
     BufferFileStream.Write(Resource, 0, Resource.Length) 
     BufferFileStream.Close() 
     Return True 

    Catch ex As Exception 
     Throw New Exception(ex.Message, ex.InnerException) 
     Return False 

    End Try 

End Function 

用法:

LoadResourceToDisk(My.Resources.Program, "C:\Program.exe") 
    Process.Start("C:\Program.exe") 

,並啓動一個現有的文件,你可以這樣做:

Private Sub Test() Handles Button1.Click 

    Dim ExePath As String = 
    IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Screen.exe") 

    Dim Result As Integer 

    Using p As New Process() 

     With p.StartInfo 
      .FileName = ExePath 
      .Arguments = "" 
     End With 

     p.Start() 
     p.WaitForExit() 

     Result = p.ExitCode 

    End Using 

    MsgBox(String.Format("ExitCode: {0}", Result)) 

End Sub 
0
File.WriteAllBytes(Application.StartupPath & "\FilesNameToSaveTo.exe", My.Resources.ResourceFilesName) 


Process.Start(Application.Startpath & "\FilesNameToSaveTo.exe") 
+0

如果你可以請編輯你的答案,並解釋你所展示的代碼是做什麼的,爲什麼/如何代碼回答這個問題,這真的可以幫助。 –