从资源启动可执行应用程序而不将其保存在某个地方

时间:2011-11-24 12:19:07

标签: vb.net visual-studio-2008

如果我的appres.exe资源中有文件(VB 2008 Application (myVbApp.exe)),我该如何从那里开始?在开始之前我不想在其他地方保存它(appres.exe),我只想要myVbApp.exe,不再需要文件。

2 个答案:

答案 0 :(得分:3)

从资源中检索EXE:

   Dim l as System.IO.Stream = Reflection.Assembly.GetEntryAssembly.GetManifestResourceStream(String.Format("{0}.{1}", "ApplicationProjectName", "YourExeName"))

然后将其保存到临时文件夹 - 示例方法:

Private Sub SaveStreamToFile(ByVal p_stream As Stream, ByVal p_fileName As String)
    Dim l_streamWriter As System.IO.FileStream = System.IO.File.Create(p_fileName)

    Try
        Dim l_bytes(65536) As Byte
        Dim l_offset As Integer = 0
        Dim l_readBytes As Integer

        Do
            l_readBytes = p_stream.Read(l_bytes, 0, 65536)
            l_streamWriter.Write(l_bytes, 0, l_readBytes)

            l_offset += l_readBytes

        Loop While (l_readBytes > 0)
        Debug.WriteLine("Num Of bytes Read: " + l_offset.ToString)

    Catch ex As Exception
       'log error
    Finally
        p_stream.Close()
        l_streamWriter.Close()
    End Try
End Sub

现在您在临时文件夹中获得了EXE,您可以使用以下方式定期午餐:

Process.Start("EXE_PATH")

并在完成此过程后将其删除。

答案 1 :(得分:0)

你可以试试这个:

 Dim SetupPath As String = Application.StartupPath & "\appres.exe"
            Using sCreateMSIFile As New FileStream(SetupPath, FileMode.Create)
                sCreateMSIFile.Write(My.Resources.yourexefilename, 0, My.Resources.Yourexefilename.Length)
            End Using
            Process.Start(SetupPath)