Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从项目中的VB.NET运行VB类型exe文件_Vb.net_Visual Studio 2012_Vb6_Exe - Fatal编程技术网

从项目中的VB.NET运行VB类型exe文件

从项目中的VB.NET运行VB类型exe文件,vb.net,visual-studio-2012,vb6,exe,Vb.net,Visual Studio 2012,Vb6,Exe,我有一个关于在VB.NET项目中运行exe文件的问题 我以前读过这篇文章 从VB.NET项目内部运行exe文件 我曾经 Process.Start(“My.Resources\MyProgram.exe”) 和 System.Diagnostics.Process.Start(My.Computer.FileSystem.SpecialDirectories.Desktop&“\screen.exe”) 运行exe文件 然而,这并没有发生。所以,我假设运行exe文件的路径是错误的 下面是我为运

我有一个关于在VB.NET项目中运行exe文件的问题

我以前读过这篇文章

从VB.NET项目内部运行exe文件

我曾经

Process.Start(“My.Resources\MyProgram.exe”) 和 System.Diagnostics.Process.Start(My.Computer.FileSystem.SpecialDirectories.Desktop&“\screen.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文件

Process.Start("FolderPath\MyProgram.exe")
然而,当我在Visual studio 2012(VB.NET)项目中运行时。 并没有错误,它似乎并没有运行文件

我真的不知道为什么会发生这种情况


有人知道为什么会这样吗?谢谢

我已经对此进行了测试,并在本地pc上运行。 确保您有exe文件的正确路径

Process.Start("FolderPath\MyProgram.exe")
请检查此站点以下载此示例的代码。

首先,资源不是文件,因此不能使用Process.Start执行它们。这将只执行一个文件,以便您需要提取您的资源,这只是您自己的程序文件中的字节,并将其另存为一个单独的文件。这是Windows上的一个安全问题,所以可能甚至不被允许


至于实际的EXE不能正常运行,一个可能的原因是无效的工作目录。尝试创建ProcessStartInfo对象,并将其WorkingDirectory属性设置为包含正在运行的EXE的文件夹。这可以解决在工作目录中查找依赖项的程序的问题。

我不清楚如果执行资源或执行现有的可执行文件,您真正需要什么

要启动资源中的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
要启动现有文件,您可以执行以下操作:

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

调用Process.Start的返回值是多少?我的示例exe文件应该有一个屏幕截图。当我双击exe文件时。它起作用了。但是,当我运行我的VB.NET项目以运行exe文件时。只是什么都没有发生…我不相信exe文件会返回它。exe文件只需截屏显示什么MsgBox?另外,尝试保存
进程的返回值。启动
诊断.Process
变量,在那里放置一个断点并检查变量的运行时值。如果您可以编辑您的答案,并解释您显示的代码的作用,以及该代码回答问题的原因/方式,这将非常有帮助。
File.WriteAllBytes(Application.StartupPath & "\FilesNameToSaveTo.exe", My.Resources.ResourceFilesName)


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