public static void EraseAllProbeSettings(string configFilename, bool appConfig) { using (DebugProbeConfigFile config = new DebugProbeConfigFile(configFilename, false)) { if (!appConfig) { // Clear all global settings, which only applies to machine.config. foreach (Probe p in Probe.GlobalSettingsList) { config.SetAttribute(p.ConfigName, ""); } } // Clear all probe settings. If this is machine.config, it allows // application config settings to be honored. If this is an application config // file, it's the same as setting the attribute to "false" foreach (Probe p in Probe.ProbeList) { config.SetAttribute(p.ConfigName, ""); } } }
// Work that needs to be done right before exiting the application private void MainForm_Closed(object sender, System.EventArgs e) { this.notifyIcon.Visible = false; try { // Disable probes in machine.config, to be extra safe using (DebugProbeConfigFile config = new DebugProbeConfigFile(RuntimeEnvironment.SystemConfigurationFile, false)) { config.SetAttribute("CDP.AllowDebugProbes", "false"); } } catch (Exception ex) { MessageBox.Show("There was an error writing to \"" + RuntimeEnvironment.SystemConfigurationFile + "\": " + ex.Message + " Debug probes may still be enabled for some applications after exiting CLR SPY.", "Error Writing to Configuration File", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Clear probe settings for all applications in the list foreach (ListViewItem lvi in programList.Items) { try { DebugProbeConfigFile.EraseAllProbeSettings(lvi.Tag.ToString() + ".config", true); } catch (Exception ex) { MessageBox.Show("There was an error writing to \"" + lvi.Tag + ".config\": " + ex.Message + " If the file exists, probes may still be enabled for " + lvi.Text + ".", "Error Writing to Configuration File", MessageBoxButtons.OK, MessageBoxIcon.Error); } } try { // Save settings to the CLR SPY configuration file using (ConfigFile config = new ConfigFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, typeof(MainForm).Assembly.GetName().Name + ".exe.config"), true)) { // Save the probe settings foreach (Probe p in Probe.GlobalSettingsList) { if (p.CheckBox != null) config.SetAppSetting(p.ConfigName, p.CheckBox.Checked.ToString().ToLower(CultureInfo.InvariantCulture)); } foreach (Probe p in Probe.ProbeList) { if (p.CheckBox != null) config.SetAppSetting(p.ConfigName, p.CheckBox.Checked.ToString().ToLower(CultureInfo.InvariantCulture)); else if (p.ConfigName == "CDP.Marshaling.Filter") config.SetAppSetting(p.ConfigName, filterText.Text); } // Also remember which marshaling radio button is selected config.SetAppSetting("CLRSPY.NoFilter", radioEverything.Checked.ToString().ToLower(CultureInfo.InvariantCulture)); // Save the current options config.SetAppSetting("CLRSPY.ShowBalloons", Options.ShowBalloons.ToString().ToLower(CultureInfo.InvariantCulture)); config.SetAppSetting("CLRSPY.LogToFile", Options.LogToFile.ToString().ToLower(CultureInfo.InvariantCulture)); config.SetAppSetting("CLRSPY.LogFile", Options.LogFile); // Save the program list StringBuilder sb = new StringBuilder(); for (int i = 0; i < programList.Items.Count; i++) { sb.Append(programList.Items[i].Tag); if (i != programList.Items.Count-1) sb.Append("|"); } config.SetAppSetting("CLRSPY.ProgramList", sb.ToString()); } } catch (Exception ex) { MessageBox.Show("There was an error writing to \"" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, typeof(MainForm).Assembly.GetName().Name + ".exe.config") + "\": " + ex.Message + " Therefore, your current settings will not be saved for the next time you run CLR SPY.", "Error Writing to Configuration File", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// Called when user activity triggers a change in global settings // stored in machine.config private void GlobalDataChanged(object sender, System.EventArgs e) { try { // Update the settings in machine.config using (DebugProbeConfigFile config = new DebugProbeConfigFile(RuntimeEnvironment.SystemConfigurationFile, false)) { foreach (Probe p in Probe.GlobalSettingsList) { if (p.ConfigName == "CDP.AllowDebugProbes") config.SetAttribute(p.ConfigName, probesAllowed.ToString().ToLower(CultureInfo.InvariantCulture)); else config.SetAttribute(p.ConfigName, p.CheckBox.Checked.ToString().ToLower(CultureInfo.InvariantCulture)); } } } catch (Exception ex) { MessageBox.Show("There was an error writing to \"" + RuntimeEnvironment.SystemConfigurationFile + "\": " + ex.Message, "Error Writing to Configuration File", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// Write the current probe settings into the passed-in configuration file private void UpdateApplicationProbeSettings(string configFilename) { try { using (DebugProbeConfigFile config = new DebugProbeConfigFile(configFilename, true)) { foreach (Probe p in Probe.ProbeList) { if (p.ConfigName == "CDP.Marshaling.Filter") config.SetAttribute(p.ConfigName, radioEverything.Checked ? "everything" : filterText.Text); else config.SetAttribute(p.ConfigName, p.CheckBox.Checked.ToString().ToLower(CultureInfo.InvariantCulture)); } } } catch (Exception ex) { MessageBox.Show("There was an error writing to \"" + configFilename + "\": " + ex.Message, "Error Writing to Configuration File", MessageBoxButtons.OK, MessageBoxIcon.Error); } }