从项目中的 VB.NET 运行 VB 类型的 exe 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21869228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
run VB type exe file from VB.NET from project
提问by Bob Ma
I have a question about running exe file in VB .NET project.
我有一个关于在 VB .NET 项目中运行 exe 文件的问题。
I previously read article http://www.dreamincode.net/forums/topic/244460-how-to-run-a-exe-file-from-inside-a-vs2010-project/
我以前读过文章 http://www.dreamincode.net/forums/topic/244460-how-to-run-a-exe-file-from-inside-a-vs2010-project/
to run exe file from inside of VB.NET project.
从 VB.NET 项目内部运行 exe 文件。
I used
我用了
Process.Start("My.Resources\MyProgram.exe") and System.Diagnostics.Process.Start(My.Computer.FileSystem.SpecialDirectories.Desktop & "\screen.exe")
Process.Start("My.Resources\MyProgram.exe") 和 System.Diagnostics.Process.Start(My.Computer.FileSystem.SpecialDirectories.Desktop & "\screen.exe")
to run exe file
运行exe文件
however, it doesn't have any happen. Therefore, I assume that path was wrong to run exe file
然而,它没有发生。因此,我认为运行exe文件的路径是错误的
here is what i did for running exe file
这是我为运行 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
I manually click exe file , it was running properly. Moreover, I use if statement to make sure if there is file or not
我手动单击 exe 文件,它运行正常。此外,我使用 if 语句来确定是否有文件
it returns true which means there is a exe file.
它返回true,这意味着有一个exe文件。
However, when I run in Visual studio 2012 (VB.NET ) project. There is no error and it doesn't seem to run file.
但是,当我在 Visual Studio 2012 (VB.NET) 项目中运行时。没有错误,它似乎没有运行文件。
I am not really sure why this happen
我不确定为什么会发生这种情况
Does anybody know why this happen ? thank
有人知道为什么会这样吗?谢谢
回答by Deddy H
i already test for this, and it run in my local pc. Make sure you have correct path for exe file.
我已经对此进行了测试,它在我的本地电脑上运行。确保您有正确的 exe 文件路径。
Process.Start("FolderPath\MyProgram.exe")
please check this site to download the code example for this. Example Code
请检查此站点以下载此代码示例。 示例代码
回答by jmcilhinney
Firstly, resources are not files so you can't execute them using Process.Start. That will execute a file only so you would need to extract your resource, which is just bytes within your own program file, and save it as a separate file. That's a security issue on Windows though, so may not even be allowed.
首先,资源不是文件,因此您不能使用 Process.Start 执行它们。这只会执行一个文件,因此您需要提取您的资源,这只是您自己程序文件中的字节,并将其另存为一个单独的文件。不过,这是 Windows 上的安全问题,因此甚至可能不被允许。
As for an actual EXE not running properly, one possible cause is an invalid working directory. Try creating a ProcessStartInfo object and setting its WorkingDirectory property to the folder containing the EXE that you're running. That can solve issues with programs that look in their working directory for dependencies.
至于实际的 EXE 运行不正常,一个可能的原因是工作目录无效。尝试创建一个 ProcessStartInfo 对象并将其 WorkingDirectory 属性设置为包含您正在运行的 EXE 的文件夹。这可以解决在其工作目录中查找依赖项的程序的问题。
回答by ElektroStudios
Is not clear for me what you really need, if execute a resource or execute an existing executable file.
如果执行资源或执行现有的可执行文件,我不清楚您真正需要什么。
To start an exe that is in your resources you can load it to disk like this:
要启动资源中的 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
Usage:
用法:
LoadResourceToDisk(My.Resources.Program, "C:\Program.exe")
Process.Start("C:\Program.exe")
And to start a existing file you can do:
要启动现有文件,您可以执行以下操作:
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
回答by user4582488
File.WriteAllBytes(Application.StartupPath & "\FilesNameToSaveTo.exe", My.Resources.ResourceFilesName)
Process.Start(Application.Startpath & "\FilesNameToSaveTo.exe")

