在切换代理之前提示用户

时间:2016-07-06 12:48:11

标签: vbscript proxy scripting

我需要一些VBScript帮助来禁用和启用代理设置。我想要发生的是脚本告诉用户代理的当前设置是关闭还是打开然后如果他们想要它们可以点击你想改变是/否。或者我想说代理现已关闭或代理现在已开启。

我知道如何制作一个消息框我不知道应该把代码放在哪里。

这是我的文本框代码:

result = Msgbox("Proxy is now set to off", vbOKonly+vbInformation, "")

这是代理更改代码:

Option Explicit 
Dim WSHShell, strSetting
Set WSHShell = WScript.CreateObject("WScript.Shell")

'Determine current proxy setting and toggle to oppisite setting
strSetting = WSHShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
If strSetting = 1 Then
  NoProxy
Else
End If

'Subroutine to Toggle Proxy Setting to ON
Sub Proxy 
  WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD" 
End Sub

'Subroutine to Toggle Proxy Setting to OFF
Sub NoProxy 
  WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
End Sub

1 个答案:

答案 0 :(得分:1)

我已经重新组织了一些代码但是这应该允许您选择是否要打开或关闭代理。

一些变化包括;

  • 移动不会更改为Constant的重复使用的字符串。
  • 通过将主逻辑包装在名为Main()的子过程中,为脚本提供单点执行。然后,我们确保对该过程的调用是我们在全局范围内运行的唯一代码,但WScript.Shell声明除外。
  • CurrentProxy()的返回值转换为布尔值可以更轻松地切换关闭和打开行为。
  • 使用Array存储MsgBox()中使用的字词变体,以避免重复代码。
Option Explicit 
Dim WSHShell, strSetting
Set WSHShell = WScript.CreateObject("WScript.Shell")

'Store strings that will not change and are reused in constants
Const APPNAME = "Proxy Setting"
Const PROXY_SETTING = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable"

'Have main procedure to be single point of script execution.
Call Main()

Sub Main()
  'Convert return value to Boolean to make it easy to do toggle.
  Dim setting: setting = CBool(CurrentProxy())
  Dim state
  'Use array to store wordy bits that will be used by the Message Box.
  If setting Then
    state = Array("enabled", "off")
  Else
    state = Array("disabled", "on")
  End If
  If MsgBox("Proxy is " & state(0) & vbCrLf & "Do you wish to switch it " & state(1), vbYesNo + vbQuestion, APPNAME) = vbYes Then
    'Toggle is opposite of current state so use Not.
    Call ToggleProxy(Not setting)
    Call MsgBox("Proxy has switched " & state(1), vbInformation, APPNAME)
  End If
End Sub

'Determine current proxy setting and toggle to oppisite setting
Function CurrentProxy()
  Dim strSetting
  strSetting = WSHShell.RegRead(PROXY_SETTING)
  CurrentProxy = strSetting
End Function

'Joined Proxy and NoProxy together into one procedure call and pass in the
'ProxyEnable setting as an argument.
'Subroutine to Toggle Proxy Setting to ON
Sub ToggleProxy(setting)
  'Abs() function makes sure we only pass back 1 or 0 as Boolean True in
  'VBScript is actually -1.
  WSHShell.RegWrite PROXY_SETTING, Abs(setting), "REG_DWORD" 
End Sub