如何从vb资源运行bat文件和自定义cmd文件?

时间:2012-04-15 07:23:50

标签: vb.net

我正在尝试使用我的资源中的cmd文件运行批处理文件。我不想在任何地方提取它们。我只想在表单中编辑bat命令并使用我拥有的cmd文件运行它。这可能吗?

1 个答案:

答案 0 :(得分:2)

无法先运行.cmd.bat文件而不将其保存到磁盘。它必须由cmd.exe来阅读和解释。您必须先将其保存到磁盘并从那里运行它。

保存后,您可以使用System.Diagnostics.Process运行它。从链接的VB.Net示例:

Imports System
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
Class MyProcess

  Public Shared Sub Main()
    Dim myProcess As New Process()

      Try                ' Get the path that stores user documents.
        myProcess.StartInfo.UseShellExecute = False
        ' You can start any process, HelloWorld is a do-nothing example.
        myProcess.StartInfo.FileName = "C:\\HelloWorld.exe"
        myProcess.StartInfo.CreateNoWindow = True
        myProcess.Start()
        ' This code assumes the process you are starting will terminate itself. 
        ' Given that is is started without a window so you cannot terminate it 
        ' on the desktop, it must terminate itself or you can do it 
        ' programmatically from this application using the Kill method.
      Catch e As Exception
        Console.WriteLine((e.Message))
      End Try
    End Sub 'Main
  End Class
End Namespace