我有一個類修改Internet設置的ProxyEnable和ProxyServer註冊表項。.NET安裝項目 - 更改註冊表設置
如果我在控制檯應用程序中使用這個類並運行它,值會發生變化,我可以使用regedit查看更改。
現在不工作的部分是我試圖在安裝程序類中使用此類,以便在安裝我的應用程序時更改註冊表項。
下面是類代碼:
public class ProxySettings {
[DllImport("wininet.dll")]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption,
IntPtr lpBuffer, int dwBufferLength);
public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
public const int INTERNET_OPTION_REFRESH = 37;
static bool settingsReturn, refreshReturn;
public void EnableProxy(string proxy) {
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", proxy);
settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED,
IntPtr.Zero, 0);
refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
}
public void DisableProxy() {
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 0);
registry.DeleteValue("ProxyServer", false);
settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED,
IntPtr.Zero, 0);
refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
}
}
這裏是我Installer類:
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer {
public WindowsServiceInstaller() {
ServiceProcessInstaller serviceProcessInstaller =
new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
//# Service Information
serviceInstaller.DisplayName = "Client Service";
serviceInstaller.StartType = ServiceStartMode.Automatic;
//# This must be identical to the WindowsService.ServiceBase name
//# set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = "Client Service";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
public override void Commit(System.Collections.IDictionary savedState) {
base.Commit(savedState);
ServiceController controller = new ServiceController("Client Service");
if(controller.Status != ServiceControllerStatus.Running) {
controller.Start();
}
ProxySettings proxy = new ProxySettings();
proxy.EnableProxy("127.0.0.1:8889");
}
public override void Uninstall(System.Collections.IDictionary savedState) {
base.Uninstall(savedState);
ProxySettings proxy = new ProxySettings();
proxy.DisableProxy();
}
}
在提交我開始工作正常服務的覆蓋,但由於某種原因,我沒有看到對註冊表的更改。
太確保類是初始化和解僱我加
System.Diagnostics.Process.Start("http://www.microsoft.com");
到兩個EnableProxy和DisableProxy方法。在安裝期間,網站打開並在卸載過程中,但註冊表保持不變。
我在做什麼錯?爲什麼它可以作爲控制檯應用程序運行,但不在安裝程序中運行?
這是有道理的 - 當您安裝「For Everyone」時,它將在所有用戶配置文件下安裝,而不是當前用戶配置文件,因此'Registry.CurrentUser'不會在您想要的位置上執行。 – Aaronaught 2010-01-18 18:48:35