Process.Start()和使用BAT文件(文件路径中的空格)

时间:2016-11-07 02:44:55

标签: vb.net batch-file process.start

我打开的应用程序只是一个基本添加规则功能的默认Windows防火墙的快速界面,因为我打开了很多端口和应用程序。

为了让我的应用程序按照我的需要运行,我需要它最终拥有管理员权限。但是,从这样开始,阻止我想要允许.exe的文件拖放。所以我需要升级流程 - 很简单。我找到了代码(.ver​​b = runas)

为了避免为每个规则(以及每个规则的管理员提示符)调用它,我决定更简单的解决方案是将规则写入.bat文件并用管理员权限来调用它。

问题在于: 我的应用程序目录是D:\ Documents \ Visual Basic \ Projects ......等等。

.bat位于应用程序目录中。但是,每当我尝试运行以下内容时:

 dim x as new processstartinfo()
 with x
.filename = (controlchars.quote + my.application.info.directoryinfo + 
"\exec.bat" + controlchars.quote)
 other settings....

 process.start(x)

我在控制台上获得的内容(因为我可以使用cmd / k使其停留)是这个错误:

 D:\Documents\Visual is not a valid command or couldn't be found.

回想一下路径是:D:\ Documents \ Visual Basic \ Projects

很明显,这个问题与那个空白有关。但是,我为我的生活找不到在线的工作解决方案。我使用了转义引号,单独的变量,controlchars.quote ....似乎没有任何效果。

我必须使用process.start才能进行升级,所以我无法解决这个问题......

如果我使用.workingdirectory,我会遇到类似的问题。

编辑:

我使用此代码的示例.bat是:

netsh advfirewall firewall add rule name="testrule1" dir=IN remoteport=2083 desc="Testing firewall rule" action=block
PAUSE

当我在我的程序的高架cmd.exe INDEPENDENT中调用它时,.bat运行完美。程序本身似乎无法找到.bat文件,这没有任何意义,因为我从写入.bat的位置复制/粘贴了它的路径。

 Private Sub btn_commit_Click(sender As Object, e As EventArgs) Handles btn_commit.Click
    Dim cmd_list As New List(Of String)
    If lb_rules_list.Items.Count = 0 Then
        MsgBox("There are no rules to add. Create one first.", MsgBoxStyle.Exclamation, "Error: No rules!")
    Else
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter(My.Application.Info.DirectoryPath + "\log.txt", True)
        For Each i As firewall_rule In lb_rules_list.Items
            Dim cmd As String = ""
            If i.path <> "" Then
                cmd = "netsh advfirewall firewall add rule name=" + Chr(34) + i.name + Chr(34) + " dir=" + i.direction + " program=" + Chr(34) + i.path + Chr(34) + " action=" + i.action.ToLower
                If i.desc <> "" Then
                    cmd = cmd + " desc=" + ControlChars.Quote + i.desc.ToString + ControlChars.Quote
                End If

            Else
                If i.port_type <> "" Then
                    cmd = "netsh advfirewall firewall add rule name=" + Chr(34) + i.name + Chr(34) + " dir=" + i.direction + " remoteport=" + Chr(34) + i.ports + Chr(34) + " action=" + i.action.ToLower +
                 " protocol=" + i.port_type
                    If i.desc <> "" Then
                        cmd = cmd + " desc=" + ControlChars.Quote + i.desc.ToString + ControlChars.Quote
                    End If
                End If
            End If
            Dim wrstr As String = ""
            wrstr = "NAME: " + i.name + ControlChars.NewLine
            If i.desc <> "" Then
                wrstr = wrstr + "        DESCRIPTION" + ControlChars.NewLine + "        " + i.desc + ControlChars.NewLine
            End If
            wrstr = wrstr + "        TYPE: " + If(i.path <> "", "Application", "Port(s)") + ControlChars.NewLine
            wrstr = wrstr + "        Direction: " + i.direction + ControlChars.NewLine
            If i.path <> "" Then
                wrstr = wrstr + "        Path: " + i.path + ControlChars.NewLine
            Else
                wrstr = wrstr + "        Port(s): " + i.ports + ControlChars.NewLine + "        Protocol: " + i.port_type + ControlChars.NewLine
            End If
            wrstr = wrstr + ControlChars.NewLine
            file.Write(wrstr)
            cmd_list.Add(cmd)
        Next
        file.Close()
        Using batwriter As New IO.StreamWriter(My.Application.Info.DirectoryPath + "\exec.bat")
            For Each c As String In cmd_list
                batwriter.WriteLine(c)
            Next
            batwriter.WriteLine("PAUSE")
            batwriter.Flush()
            batwriter.Close()

        End Using
        Try
            Dim proc As New ProcessStartInfo()
            With proc
                '.WindowStyle = ProcessWindowStyle.Hidden
                .UseShellExecute = True
                .FileName = ("""" + My.Application.Info.DirectoryPath + "\exec.bat""")
                .Verb = "runas"
            End With
            Process.Start(proc)
        Catch ex As Exception
            MsgBox("Process failed: " + ex.Message)
        End Try
        My.Computer.FileSystem.DeleteFile(My.Application.Info.DirectoryPath + "\exec.bat")
        lb_rules_list.Items.Clear()
        rb_prt_tcp.Checked = False
        rb_prt_udp.Checked = False
        For Each i As TextBox In Me.Controls.OfType(Of TextBox)
            i.Text = ""
        Next
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

尝试3个转义引号。

当我这样做时,这对我有用:

.Filename = "cmd.exe"
.Arguments = "/k " +  """""" + My.Application.Info.DirectoryPath + "\exec.bat" + """"""
.UseShellExecute = True
.Verb = "runas"