/// <summary> /// Enumerate all devices and optionally uninstall ghosted ones. /// </summary> /// /// <param name="RemoveGhosts"> true if ghosted devices should be uninstalled. </param> /// <param name="HideUnfiltered"> true to hide, false to show the unfiltered. </param> private void Enumerate(Boolean RemoveGhosts, Boolean HideUnfiltered = false) { using (new WaitCursor()) { toolStripStatusLabel1.Text = String.Empty; toolStripStatusLabel2.Text = String.Empty; toolStripStatusLabel3.Text = String.Empty; toolStripStatusLabel4.Text = String.Empty; //! Add a Overlay here? Buster.Enumerate(); Enabled = false; StringBuilder sb = new StringBuilder(); toolStripProgressBar1.Value = 0; ReColorDevices(true); Int32 fndx = -1; try { listView1.BeginUpdate(); listView1.Items.Clear(); listView1.Groups.Clear(); using (IniFile ini = new IniFile(Buster.IniFileName)) { foreach (HwEntry he in Buster.HwEntries) { //Use Insert instead of Add... ListViewItem lvi = listView1.Items.Add(he.Description); for (int j = 1; j < listView1.Columns.Count; j++) { lvi.SubItems.Add(""); } if (he.Properties.ContainsKey(SPDRP.MFG.ToString())) { lvi.SubItems[(int)LVC.ManuCol].Text = he.Properties[SPDRP.MFG.ToString()]; } foreach (ListViewGroup lvg in listView1.Groups) { if (lvg.Name == he.DeviceClass) { lvg.Items.Add(lvi); break; } } if (lvi.Group == null) { //Use Insert instead of Add... foreach (ListViewGroup lvg in listView1.Groups) { if (String.Compare(lvg.Name, he.DeviceClass, true) >= 0) { Int32 ndx = listView1.Groups.IndexOf(lvg); listView1.Groups.Insert(ndx, new ListViewGroup(he.DeviceClass, String.IsNullOrEmpty(he.DeviceClass) ? "<No device class specified>" : he.DeviceClass)); listView1.Groups[ndx].Items.Add(lvi); break; } } } if (lvi.Group == null) { Int32 ndx = listView1.Groups.Add(new ListViewGroup(he.DeviceClass, he.DeviceClass)); listView1.Groups[ndx].Items.Add(lvi); } lvi.SubItems[(int)LVC.StatusCol].Text = he.DeviceStatus; lvi.SubItems[(int)LVC.DescriptionCol].Text = he.FriendlyName; lvi.Tag = he; if (RemoveGhosts && he.RemoveDevice_IF_Ghosted_AND_Marked()) { lvi.SubItems[(int)LVC.StatusCol].Text = "REMOVED"; fndx = lvi.Index; if (toolStripProgressBar1.Value < toolStripProgressBar1.Maximum) { toolStripProgressBar1.Increment(1); } statusStrip1.Invalidate(); Application.DoEvents(); } Application.DoEvents(); } } } finally { Enabled = true; } listView1.Columns[(int)LVC.DeviceCol].Width = -1; listView1.Columns[(int)LVC.StatusCol].Width = -1; listView1.Columns[(int)LVC.DescriptionCol].Width = -1; listView1.EndUpdate(); if (fndx != -1) { listView1.EnsureVisible(fndx); } ReColorDevices(false); //Remove all devices with just Ok/Service and No Match if (HideUnfiltered) { for (Int32 i = listView1.Items.Count - 1; i >= 0; i--) { if ((listView1.Items[i].SubItems[(int)LVC.StatusCol].Text.Equals("Ok") || listView1.Items[i].SubItems[(int)LVC.StatusCol].Text.Equals("Service")) && String.IsNullOrEmpty(listView1.Items[i].SubItems[(int)LVC.MatchTypeCol].Text)) { listView1.Items.RemoveAt(i); } } } ReColorDevices(false); if (RemoveGhosts && toolStripProgressBar1.Value != 0) { toolStripProgressBar1.Value = toolStripProgressBar1.Maximum; } //foreach (ListViewGroup lvg in listView1.Groups) //{ // if (String.IsNullOrEmpty(lvg.Header)) // { // lvg.Header = "<No Class Specified>"; // } //} } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //! Add Support for CommandLine Options: //! //! /Remove //! /Register (Task) //! /Unregister (Task) //! IniFile //! //! Start logging to TaskManager Log, EventLog and Debug Console. switch (Environment.GetCommandLineArgs().Length) { case 3: if (Buster.IsAdmin()) { if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/NOGUI")) { String fn = Environment.ExpandEnvironmentVariables(Environment.GetCommandLineArgs()[2]); if (File.Exists(fn) && fn.ToUpper().EndsWith(".INI")) { using (Buster buster = new Buster(fn)) { Buster.Enumerate(); Buster.WriteToEventLog(String.Format("Found {0} Device(s)", Buster.HwEntries.Count), EventLogEntryType.Information); Buster.WriteToEventLog(String.Format("Loaded {0} Device Filter(s)", Buster.Devices.Count), EventLogEntryType.Information); Buster.WriteToEventLog(String.Format("Loaded {0} Class Filter(s)", Buster.Classes.Count), EventLogEntryType.Information); Buster.WriteToEventLog(String.Format("Loaded {0} Wildcard Filter(s)", Buster.Wildcards.Count), EventLogEntryType.Information); //! Count Filtered Devices (separate match from deletion in RemoveDevice_IF_Ghosted_AND_Marked). Int32 ghosted = 0; Int32 cnt = 0; foreach (HwEntry he in Buster.HwEntries) { //Debug.WriteLine(he.Description); if (he.Ghosted) { ghosted++; } if (he.RemoveDevice_IF_Ghosted_AND_Marked()) { cnt++; } } Buster.WriteToEventLog(String.Format("Removed {0} of {1} Ghosted Device(s).", cnt, ghosted), EventLogEntryType.Information); Application.Exit(); return; } } else { MessageBox.Show(String.Format("Configuration File not found at \r\n'{0}'.", fn), Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/REGISTER")) { // Get the service on the local machine using (TaskService ts = new TaskService()) { String user = System.Security.Principal.WindowsIdentity.GetCurrent().Name; String conf = Environment.ExpandEnvironmentVariables(Environment.GetCommandLineArgs()[2]); String appl = Application.ExecutablePath; Debug.WriteLine(appl); Debug.WriteLine(conf); Version ver = ts.HighestSupportedVersion; Boolean newVer = (ver >= new Version(1, 2)); TaskDefinition td = ts.NewTask(); td.Data = "GhostBuster"; td.Principal.UserId = user; td.Principal.LogonType = TaskLogonType.InteractiveToken; td.RegistrationInfo.Author = "Wim van der Vegt"; td.RegistrationInfo.Description = String.Format( "{0} removes selected Ghosted Devices. \r\n" + "\r\n" + "Default Settings for this scheduled task are:\r\n" + " - Running with the required Administrative Privileges,\r\n" + " - Running 1 minute after a user logs on,\r\n" + " - Can be started manually with 'schtasks /run /ts {0}'. \r\n" + "\r\n" + "See http://ghosutbuster.codeplex.com", Buster.S_TITLE); td.RegistrationInfo.Documentation = "See http://ghostbuster.codeplex.com"; td.Settings.DisallowStartIfOnBatteries = true; td.Settings.Hidden = false; if (newVer) { td.Principal.RunLevel = TaskRunLevel.Highest; td.RegistrationInfo.Source = Buster.S_TITLE; td.RegistrationInfo.URI = new Uri("http://ghostbuster.codeplex.com"); td.RegistrationInfo.Version = new Version(0, 9); td.Settings.AllowDemandStart = true; td.Settings.AllowHardTerminate = true; td.Settings.Compatibility = TaskCompatibility.V2; td.Settings.MultipleInstances = TaskInstancesPolicy.Queue; } LogonTrigger lTrigger = (LogonTrigger)td.Triggers.Add(new LogonTrigger()); if (newVer) { lTrigger.Delay = TimeSpan.FromMinutes(1); lTrigger.UserId = user; } td.Actions.Add(new ExecAction( appl, String.Format("/NOGUI {0}", conf), Path.GetDirectoryName(appl))); //if (ts.FindTask(Buster.S_TITLE) != null) //{ // ts.RootFolder.DeleteTask(Buster.S_TITLE); //} // Register the task in the root folder Task task = ts.RootFolder.RegisterTaskDefinition( Buster.S_TITLE, td, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null); task.ShowEditor(); } } } else { MessageBox.Show(S_CONSOLE, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } break; case 2: { if (Buster.IsAdmin()) { if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/UNREGISTER")) { using (TaskService ts = new TaskService()) { if (ts.FindTask(Buster.S_TITLE) != null) { ts.RootFolder.DeleteTask(Buster.S_TITLE); } } } else if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/EVENTLOG")) { if (!EventLog.SourceExists(Buster.S_TITLE)) { EventLog.CreateEventSource(Buster.S_TITLE, Buster.S_TITLE); } } else { MessageBox.Show(S_SYNTAX, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } Application.Exit(); return; } else { if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/REGISTER") || Environment.GetCommandLineArgs()[1].ToUpper().Equals("/UNREGISTER")) { MessageBox.Show(S_SCHEDULE, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/NOGUI")) { MessageBox.Show(S_CONSOLE, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (Environment.GetCommandLineArgs()[1].ToUpper().Equals("/EVENTLOG")) { MessageBox.Show(S_EVENTLOG, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(S_SYNTAX, Buster.S_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information); } } } break; default: Application.Run(new Mainform()); break; } }