我正在製作的應用程序僅僅是默認Windows防火牆的一些快速界面,用於某些基本的添加規則功能,因爲我打開了很多端口並應用。Process.Start()和使用BAT文件(文件路徑中的空格)
爲了讓我的應用程序工作得如何,我需要它最終擁有管理員權限。然而,像這樣開始,可以防止我想要允許的.exe文件拖放。所以我需要升級一個流程 - 很簡單。我發現它的代碼(.verb = runas)
爲了避免爲每個規則調用它(並因此爲每個規則的管理提示符),我決定更簡單的解決方案是將規則寫入.bat文件並用管理權限來調用它。
問題的存在: 我的應用程序目錄是d:\文檔\ Visual Basic中\項目......等等,等等
將.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。
編輯:
蝙蝠我這個代碼中使用的一個例子是:
netsh advfirewall firewall add rule name="testrule1" dir=IN remoteport=2083 desc="Testing firewall rule" action=block
PAUSE
的蝙蝠完美運行時,我把它在我的程序的升高CMD.EXE獨立的。程序本身似乎無法找到.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
你不應該需要把引號'x.FileName'。也許錯誤來自你的.bat文件。 – Blorgbeard
@Blorgbeard 2:19 .BAT如下: 的netsh advfirewall防火牆添加規則(規則定義) PAUSE 如果我直接運行它,它完美的作品。只有當我通過流程調用它時 - 這是因爲這個問題導致無法找到bat文件。 –
您是否試過'.filename =(「」+ my.application.info.directoryinfo + 「\ exec.bat」「」)'? – soja