以编程方式更改网络界面,而无需管理员提升

时间:2018-07-27 13:54:46

标签: vb.net network-programming visual-studio-2017 netsh elevated-privileges

我有一个小工具(用VB编写),允许用户保存多个静态IP配置(用于需要静态IP的不同位置)。然后,他们可以从下拉菜单中进行选择,并自动为该位置设置其界面配置。

问题在于它需要管理员权限(提升)才能运行。

我们已将用户添加到“网络配置操作员”组,并授予他们(无管理员权限)修改其界面的能力。

该程序在后台使用“ netsh”,但它运行并发挥作用,就好像它起作用了一样,但是却没有任何改变。如果右键单击并以“以管理员身份运行”,它将失败,提示需要提升。但是,您可以手动修改设置。它提示输入密码,但不会提升密码。

我已经包含了更改下面界面的代码的相关部分。

任何帮助将不胜感激。

 Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click


        Dim stInterface As String = cboInterface.Text
        Dim bReady As Boolean = False

        Dim stCommand1 As String = ""
        Dim stCommand2 As String = ""
        Dim stCommand3 As String = ""

        '//https://www.howtogeek.com/103190/change-your-ip-address-from-the-command-prompt/

        If cboStore.Text <> cDHCPString Then

            Dim NetIP As NetworkIPStructure = GetIPInfo(cboStore.Text)

            stCommand1 = "netsh interface ip set address name=""" & stInterface & """ static " & NetIP.IPAddress & " " & NetIP.SubNetMask & " " & NetIP.GateWay & " 1"

            If NetIP.PrimaryDNS.Length > 1 Then
                stCommand2 = "netsh interface ip set dns name=""" & stInterface & """ static " & NetIP.PrimaryDNS
            End If

            If NetIP.SecondaryDNS.Length > 1 Then
                stCommand3 = "netsh interface ip add dns name=""" & stInterface & """ addr=" & NetIP.SecondaryDNS & " index=2"
            End If

            bReady = True
        Else
            stCommand1 = "netsh interface ip set address """ & stInterface & """ source=dhcp"
            stCommand2 = "netsh interface ip set dns """ & stInterface & """ source=dhcp"
            bReady = True
        End If

        If bReady Then
            Me.Enabled = False
            If Len(stCommand1) > 1 Then
                '//MsgBox(stCommand1)
                Shell(stCommand1, AppWinStyle.Hide, True, -1)
            End If

            If Len(stCommand2) > 1 Then
                System.Threading.Thread.Sleep(500)
                '//MsgBox(stCommand2)
                Shell(stCommand2, AppWinStyle.Hide, True, -1)
            End If

            If Len(stCommand3) > 1 Then
                System.Threading.Thread.Sleep(500)
                '//MsgBox(stCommand3)
                Shell(stCommand3, AppWinStyle.Hide, True, -1)
            End If

            Me.Enabled = True

            Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
            Me.Text = h.AddressList.GetValue(0).ToString
            btnCancel.Text = "Close"
        End If
    End Sub

0 个答案:

没有答案