C# wmi 查询异常

C# wmi query exception

我的应用程序中有一个文本框,它使用 wmi 查询和注册表读取显示一些系统信息。问题是某些系统不显示某些信息,因为 OS(处理器:不可用等)中的损坏,因此无法使用我的方法或从 [=] 中检索该信息15=] 就此而言。 所有这些都会导致我的应用程序崩溃。 我将如何避免崩溃并为无法检索的每一行显示一个文本。例如:不可用 我的文本框代码是:

private void TextBox1_Initialized(object sender, EventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.WorkerReportsProgress = true;
        bw.DoWork += new DoWorkEventHandler(delegate (object o, DoWorkEventArgs args)
        {

        });
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate (object o, RunWorkerCompletedEventArgs args)
        {
            TextBox1.Text = Registry.GetValue(@"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor[=10=]", "ProcessorNameString", null).ToString().Replace("(R)", "").Replace("(TM)", "") + Environment.NewLine;
            TextBox1.Text += "Memory: " + getRAMsize() + Environment.NewLine;
            TextBox1.Text += "Free Space: " + GetTotalFreeSpace(sysdrive) + " GB" + Environment.NewLine;
            TextBox1.Text += diskname() + Environment.NewLine;
            if (Is64BitSystem)
            {
                TextBox1.Text += getOS() + " 64bit" + Environment.NewLine;
            }
            else
            {
                TextBox1.Text += getOS() + " 32bit" + Environment.NewLine;
            }
            Clipboard.SetDataObject(TextBox1.Text);
        });
        bw.RunWorkerAsync();
    }

提前致谢。

使用 try catch 避免应用程序崩溃:

 string ProcessorNameString = "ProcessorNameString: not available";
        string memory = "Memory: not availbale ";
        string freeSpace = "Free space: not available";
        //add others 
                   try
        {
            ProcessorNameString = Registry.GetValue(@"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor[=10=]", "ProcessorNameString", null).ToString().Replace("(R)", "").Replace("(TM)", "") + Environment.NewLine;
        }
        catch{}

        try
        {
            memory = string.Format("Free Space:{0}DB", getRAMsize());
        }
        catch { }

        try
        {
            //freeSpace here 
        }
        catch { }
        TextBox1.Text = string.Format("{0}{1}{2}{3}{4}{5}"
            , ProcessorNameString, Environment.NewLine
            , memory, Environment.NewLine
            , freeSpace, Environment.NewLine);
        Clipboard.SetDataObject(TextBox1.Text);