Tyler Adams

Tyler Adams

  • NA
  • 10
  • 0

Help cleaning up C# code (WMI)

Mar 17 2009 6:07 PM

I'm building an InfoPath form that is going to query the users system using WMI so that we can start to do an inventory of a users systems and submit that information to SharePoint.

I'm using the WMI code creator tool from Microsoft to get my code samples in C#.  I've got the code to work for me so far, but it's starting to look really ugly.  i know there's got to be a way to clean up the code.  Unfortunately I'm not a code person, I just pick code samples and beat them up until i get them to work, but have no finesse, if someone could help me clean up the code a bit i would really appreciate it.

here's the code (I'm going to be querying a bunch more hardware so i wanted to get this cleaned up early on, once i see how it's done I could work on it from there.)

using Microsoft.Office.InfoPath;

using System;

using System.Xml;

using System.Xml.XPath;

using System.Management;

//using System.Windows.Forms;

namespace Inventory_Rev1

{

public partial class FormCode

{

// Member variables are not supported in browser-enabled forms.

// Instead, write and read these values from the FormState

// dictionary using code such as the following:

//

// private object _memberVariable

// {

// get

// {

// return FormState["_memberVariable"];

// }

// set

// {

// FormState["_memberVariable"] = value;

// }

// }

// NOTE: The following procedure is required by Microsoft Office InfoPath.

// It can be modified using Microsoft Office InfoPath.

public void InternalStartup()

{

EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);

}

public void FormEvents_Loading(object sender, LoadingEventArgs e)

{

ManagementObjectSearcher searcherRAM =

new ManagementObjectSearcher("root\\CIMV2",

"SELECT * FROM Win32_PhysicalMemory");

//ManagementObjectCollection oReturnCollection = searcher.Get();

ManagementObjectSearcher searcherDisplay =

new ManagementObjectSearcher("root\\CIMV2",

"SELECT * FROM Win32_VideoController");

ManagementObjectSearcher searcherAudio =

new ManagementObjectSearcher("root\\CIMV2",

"SELECT * FROM Win32_SoundDevice");

ManagementObjectSearcher searcherNIC =

new ManagementObjectSearcher("root\\CIMV2",

"SELECT * FROM Win32_NetworkAdapter");

//Display Audio Controller Information

foreach (ManagementObject queryObj in searcherAudio.Get())

{

string audioController = queryObj["Name"].ToString();

// Create an XPathNavigator to walk the main data source

// of the form.

XPathNavigator xnMyForm = this.CreateNavigator();

XmlNamespaceManager ns = this.NamespaceManager;

xnMyForm.SelectSingleNode("/my:myFields/my:SoundCard", ns).SetValue(audioController);

}

//Display Video Controller Information

foreach (ManagementObject queryObj in searcherDisplay.Get())

{

string displayAdapter = queryObj["Name"].ToString();

// Create an XPathNavigator to walk the main data source

// of the form.

XPathNavigator xnMyForm = this.CreateNavigator();

XmlNamespaceManager ns = this.NamespaceManager;

xnMyForm.SelectSingleNode("/my:myFields/my:VideoAdapter", ns).SetValue(displayAdapter);

}

}

}

}

Any help is appreciated!!!!!