vb.net How to pass a string with spaces to the command line

后端 未结 6 1767
轻奢々
轻奢々 2020-12-11 23:02

I am trying to call an external program using Process:

    Dim strExe As String = \"E:\\Projects\\Common Files\\mktorrent.exe\"
    Dim p As New Process
             


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 23:22

    This WORKS:

    Dim current_path, current_rulename, cmd1 as STRING
    
    current_path = "C:\this folder\file name.exe"    
    current_rulename = "file name.exe"
    
    cmd1 = "netsh advfirewall firewall add rule name = """ + current_rulename + """ dir = in action = block program = """ + current_path + """"
    cmd1 &= " & "
    cmd1 &= "netsh advfirewall firewall add rule name = """ + current_rulename + """ dir = out action = block program = """ + current_path + """"
    cmd1 &= " & pause"
    
    Process.Start("cmd", "/c " + cmd1)
    

    Basically, the variables with spaces need to be enclosed like this:

    """ + string_with_spaces + """
    

    Broken into parts:

    cmd1 = 
    "
    netsh advfirewall firewall add rule name = 
    """ + current_rulename + """
    dir=in action=block
    program=
    """ + current_path + """
    "
    

    This code joins two separate commands that use STRINGS with spaces.

提交回复
热议问题