public static bool Install() { DialogResult dialogResult = MessageBox.Show("Do you want to create a TAP network interface?", "TAP Driver Setup", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.No) { return(false); } LoadingForm splash = LoadingForm.Create("Decompressing TAP driver..."); Directory.CreateDirectory("Temp"); File.WriteAllBytes(@"Temp\TAP Driver.zip", Network_Manager.Properties.Resources.TAP_Driver); WinLib.IO.Compression.UnZip(@"Temp\TAP Driver.zip", "Temp"); // Vista driver version does not install correctly on Vista, so Win2K is used string driverPath; if (Environment.Is64BitOperatingSystem) { if (Environment.OSVersion.Version.CompareTo(new Version("6.1")) > -1) { driverPath = @"Temp\TAP Driver\Vista 64-bit\OemVista.inf"; } else { driverPath = @"Temp\TAP Driver\XP 64-bit\OemWin2k.inf"; } } else if (Environment.OSVersion.Version.CompareTo(new Version("6.1")) > -1) { driverPath = @"Temp\TAP Driver\Vista 32-bit\OemVista.inf"; } else { driverPath = @"Temp\TAP Driver\XP 32-bit\OemWin2k.inf"; } splash.UpdateStatus("Installing TAP driver ..."); if (Setupapi.InstallInfDriver(driverPath, componentID, "Network Manager TAP Adapter")) { File.Delete("TAP Driver.zip"); splash.Stop(); MessageBox.Show("TAP network interface was successfully created.", "TAP Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Information); return(true); } else { File.Delete("TAP Driver.zip"); splash.Stop(); MessageBox.Show("TAP network interface failed to create.", "TAP Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }
public static bool Uninstall() { string cfgKeyPath = @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"; string nameKeyPath = @"SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}"; string tapList = ""; RegistryKey key = Registry.LocalMachine.OpenSubKey(cfgKeyPath); foreach (string name in key.GetSubKeyNames()) { try { if ((string)Registry.LocalMachine.OpenSubKey(cfgKeyPath + @"\" + name).GetValue("ComponentId") == componentID) { string netCfgInstanceId = (string)Registry.LocalMachine.OpenSubKey(cfgKeyPath + @"\" + name).GetValue("NetCfgInstanceId"); string friendlyName = (string)Registry.LocalMachine.OpenSubKey(nameKeyPath + @"\" + netCfgInstanceId + @"\Connection").GetValue("Name"); tapList += friendlyName + "\n"; } } catch { } } key.Close(); if (tapList == "") { MessageBox.Show("No TAP interface was found.", "TAP Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Information); return(false); } DialogResult dialogResult = MessageBox.Show("This will remove the following network interfaces:\n\n" + tapList + "\nDo you want to remove all these TAP network interfaces?", "TAP Driver Setup", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.No) { return(false); } LoadingForm splash = LoadingForm.Create("Uninstalling TAP driver ..."); if (Setupapi.UninstallDevice(componentID)) { splash.Stop(); MessageBox.Show("Uninstallation was successful.", "TAP Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Information); return(true); } else { splash.Stop(); MessageBox.Show("Uninstallation failed.", "TAP Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }