patterncsharpMinor
Query a remote computer using WMI, and return the installed printers in a datagridview
Viewed 0 times
thereturnqueryanddatagridviewwmiusingcomputerprintersremote
Problem
I have an application that has multiple functions. Two in particular will query a remote computer using WMI, and return the installed printers in a datagridview. The other is a new method that will query the remote computer using WMI, and return information about the RAM.
I don't want to repeat the code that populates the datagrideview each time I need to display info from a WMI call. To accommodate this, I changed the way the datagrideview is populated. It is now in a separate class.
I'm just wanting to know if this is the best solution.
Here is one of the methods that implements the new class I made. This method below will get the installed printers on the target machine:
```
class remote_Info
{
public Form getPrinters(string target, Form print_Form)
{
/**This will query the remote computer for the installed printers.
* It does this by using WMI and looking in Win32_Printer.
*
/
// Check to make sure no null arguments were passed
if (target == null | print_Form == null)
throw new ArgumentNullException();
// Define the dataTable and DataColumns
// The columns will server as the header in the datagrid
DataTable dt = new DataTable();
DataColumn colName = new DataColumn("Name");
DataColumn col_Driver = new DataColumn("Driver");
DataColumn colLocation = new DataColumn("Location");
dt.Columns.Add(colName);
dt.Columns.Add(col_Driver);
dt.Columns.Add(colLocation);
ManagementScope scope = new ManagementScope("\\\\" + target + "\\root\\cimv2");
SelectQuery query = new SelectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query);
try
{
using (ManagementObjectCollection moc = mos.Get())
{
// Fire off a foreach loop. First creating a new data
foreach (ManagementObject mo in moc)
{
try
{
I don't want to repeat the code that populates the datagrideview each time I need to display info from a WMI call. To accommodate this, I changed the way the datagrideview is populated. It is now in a separate class.
I'm just wanting to know if this is the best solution.
Here is one of the methods that implements the new class I made. This method below will get the installed printers on the target machine:
```
class remote_Info
{
public Form getPrinters(string target, Form print_Form)
{
/**This will query the remote computer for the installed printers.
* It does this by using WMI and looking in Win32_Printer.
*
/
// Check to make sure no null arguments were passed
if (target == null | print_Form == null)
throw new ArgumentNullException();
// Define the dataTable and DataColumns
// The columns will server as the header in the datagrid
DataTable dt = new DataTable();
DataColumn colName = new DataColumn("Name");
DataColumn col_Driver = new DataColumn("Driver");
DataColumn colLocation = new DataColumn("Location");
dt.Columns.Add(colName);
dt.Columns.Add(col_Driver);
dt.Columns.Add(colLocation);
ManagementScope scope = new ManagementScope("\\\\" + target + "\\root\\cimv2");
SelectQuery query = new SelectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query);
try
{
using (ManagementObjectCollection moc = mos.Get())
{
// Fire off a foreach loop. First creating a new data
foreach (ManagementObject mo in moc)
{
try
{
Solution
First, please do what Leonid mentions in his comment. The code is woefully out of standard convention for C# and .NET. Is this a Java port perhaps? Secondly, check your spelling in your comments. Nothing makes a developer wince more than looking at someone else's code and starting off by seeing egregious spelling errors. It does not speak well for the rest of the code.
That bit out of the way, I'm going to tackle your
Best of luck.
That bit out of the way, I'm going to tackle your
display_Forms class as an example of where to start to clean up. A big part of what I did what separate concerns into separate methods so that future changes are more easily found and dealt with. Note I made the class static as you're storing no state (also take note that the remote_Info class is the same way) and I'm using an extension method on the Form so it can be used a bit more fluently.Best of luck.
internal static class DisplayForms
{
public static Form WmiFillGrid(this Form printForm, string title, DataTable dt)
{
return printForm == null ? null : UpdatePrintForm(printForm, title, CreateDataGridView(dt));
}
private static Control CreateDataGridView(DataTable dt)
{
// Declare the data grid view
return new DataGridView
{
// Data grid Properties
BorderStyle = BorderStyle.None,
BackgroundColor = SystemColors.GradientInactiveCaption,
AutoSize = true,
Width = 0,
Height = 0,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells,
// Set the data source to the Data table
DataSource = dt
};
}
private static Form UpdatePrintForm(Form printForm, string title, Control dataGrid)
{
// Set the title of the form.
printForm.Text = title;
printForm.AutoSize = true;
printForm.Width = dataGrid.Width;
printForm.Height = dataGrid.Height;
// Add the data grid to the form
printForm.Controls.Add(dataGrid);
return printForm;
}
}Code Snippets
internal static class DisplayForms
{
public static Form WmiFillGrid(this Form printForm, string title, DataTable dt)
{
return printForm == null ? null : UpdatePrintForm(printForm, title, CreateDataGridView(dt));
}
private static Control CreateDataGridView(DataTable dt)
{
// Declare the data grid view
return new DataGridView
{
// Data grid Properties
BorderStyle = BorderStyle.None,
BackgroundColor = SystemColors.GradientInactiveCaption,
AutoSize = true,
Width = 0,
Height = 0,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells,
// Set the data source to the Data table
DataSource = dt
};
}
private static Form UpdatePrintForm(Form printForm, string title, Control dataGrid)
{
// Set the title of the form.
printForm.Text = title;
printForm.AutoSize = true;
printForm.Width = dataGrid.Width;
printForm.Height = dataGrid.Height;
// Add the data grid to the form
printForm.Controls.Add(dataGrid);
return printForm;
}
}Context
StackExchange Code Review Q#11044, answer score: 2
Revisions (0)
No revisions yet.