使用自定义颜色VB.NET打开批处理文件

时间:2017-02-25 13:44:23

标签: vb.net batch-file

我尝试在自定义彩色cmd窗口中打开批处理文件:

Public Class MyUtilities
    Shared Sub RunCommandCom(command As String, arguments As String, permanent As Boolean)
        Dim p As Process = New Process()
        Dim pi As ProcessStartInfo = New ProcessStartInfo()
        pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
        pi.FileName = "cmd.exe"
        p.StartInfo = pi
        p.Start()
    End Sub
End Class

       'Inside button click event
        Dim OpenCMD
        OpenCMD = CreateObject("wscript.shell")
        OpenCMD.run("cmd.exe & color 0e & " & My.Computer.FileSystem.SpecialDirectories.Desktop & "\test.bat " & "VARIABLE", vbNormalFocus)

然而,它一直说无法找到该文件,当我从头开始删除"cmd.exe & color 0e & "时,它工作正常。由于我正在使用许多不同的批处理文件而不想更改批处理文件本身的颜色,我怎样才能更改它以使其在运行时具有某种颜色? - 批处理文件由许多不同的程序运行,因此我不想改变实际批处理文件本身的颜色,只有从我的程序运行时...我无法复制原始文件也是如此。

您的建议/更正将不胜感激。

1 个答案:

答案 0 :(得分:0)

我不确定您为什么不在按钮点击事件中使用RunCommandCom。你只需要:

Dim cmd As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "test.bat")
RunCommandCom("color 0e & " & cmd, "VARIABLE", False)

使用IO.Path.Combine为您处理路径分隔符(" \"在本例中)。

(经测试工作。)