console.writeline

G

Guest

Greetings everybody!

I have a short but hopefully simple question. If anybody can give me some
direction, I would greatly appreciate it.

I am in the process of writing a small console utility that will pull in a
computers hotfix information via wmi’s win32_QuickFixEngineering. The code
listed below works, the only bump I can’t seem to get over is the way the
data is outputted. If at all possible, can you help me output the data in a
comma delimited format? Any help would be greatly appreciated – thank you so
much in advance.

//Code:

//Authored by Thomas Mshar / Ray Cables / Brad Budgick
//Wachovia Desktop Services / Charlotte Trade Floor Support


using System;
using System.Management;

class Constants
{
public const string UserID = "test\test";
public const string Password = "test";
public const string ComputerName = ".";
public const string NameSpace = "Root\\CIMv2";
public const string WMIClass = "Win32_QuickFixEngineering";
}

class WMISample
{
public static int Main(string[] args)
{
ConnectionOptions options = new ConnectionOptions();
options.Username = Constants.UserID;
options.Password = Constants.Password;
options.Authentication = AuthenticationLevel.Default;
options.Impersonation = ImpersonationLevel.Impersonate;

ManagementScope scope = new ManagementScope();
scope.Path = new ManagementPath("\\\\" + Constants.ComputerName
+
"\\" + Constants.NameSpace);
scope.Options = options;

try {
scope.Connect();
}
catch (Exception Err)
{
Console.WriteLine("Failed to connect: " + Err.Message);
return 1;
}

ManagementClass WMIClass = new
ManagementClass(Constants.WMIClass);
WMIClass.Scope = scope;

ManagementObjectCollection WMIInstanceCollection =
WMIClass.GetInstances();

foreach (ManagementObject WMIInstance in WMIInstanceCollection)
{
Console.WriteLine();
PropertyDataCollection.PropertyDataEnumerator
propertyEnumerator =

WMIInstance.Properties.GetEnumerator();
while (propertyEnumerator.MoveNext())
{
PropertyData property =
(PropertyData)propertyEnumerator.Current;
Console.WriteLine(property.Name + " = " +
property.Value);
//Console.WriteLine(property.Name + " = " + property.Value + " = "
+ property.type);
}
}
return 0;
}
}

//END Code
 
M

Morten Wennevik

Hi Thomas,

I'm not entirely sure how you want the data output, but you could always use Console.Write instead of WriteLine, or comma delimit the strings before writing to console.

For advanced console features you need to tap into kernel32.dll.


Greetings everybody!

I have a short but hopefully simple question. If anybody can give me some
direction, I would greatly appreciate it.

I am in the process of writing a small console utility that will pull in a
computers hotfix information via wmi’s win32_QuickFixEngineering. The code
listed below works, the only bump I can’t seem to get over is the way the
data is outputted. If at all possible, can you help me output the data in a
comma delimited format? Any help would be greatly appreciated – thank you so
much in advance.

//Code:

//Authored by Thomas Mshar / Ray Cables / Brad Budgick
//Wachovia Desktop Services / Charlotte Trade Floor Support


using System;
using System.Management;

class Constants
{
public const string UserID = "test\test";
public const string Password = "test";
public const string ComputerName = ".";
public const string NameSpace = "Root\\CIMv2";
public const string WMIClass = "Win32_QuickFixEngineering";
}
class WMISample
{
public static int Main(string[] args)
{
ConnectionOptions options = new ConnectionOptions();
options.Username = Constants.UserID;
options.Password = Constants.Password;
options.Authentication = AuthenticationLevel.Default;
options.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope scope = new ManagementScope();
scope.Path = new ManagementPath("\\\\" + Constants.ComputerName
+
"\\" + Constants.NameSpace);
scope.Options = options;

try {
scope.Connect();
}
catch (Exception Err)
{
Console.WriteLine("Failed to connect: " + Err.Message);
return 1;
}

ManagementClass WMIClass = new
ManagementClass(Constants.WMIClass);
WMIClass.Scope = scope;
ManagementObjectCollection WMIInstanceCollection =
WMIClass.GetInstances();

foreach (ManagementObject WMIInstance in WMIInstanceCollection)
{
Console.WriteLine();
PropertyDataCollection.PropertyDataEnumerator
propertyEnumerator =
WMIInstance.Properties.GetEnumerator();
while (propertyEnumerator.MoveNext())
{
PropertyData property =
(PropertyData)propertyEnumerator.Current;
Console.WriteLine(property.Name + " = " +
property.Value);
//Console.WriteLine(property.Name + " = " + property.Value + " = "
+ property.type);
}
}
return 0;
}
}

//END Code
 
B

Bill Butler

Thomas M. said:
Greetings everybody!
The code
listed below works, the only bump I can't seem to get over is the way the
data is outputted. If at all possible, can you help me output the data in
a
comma delimited format? Any help would be greatly appreciated - thank you
so
much in advance.

Hi,

Try using formatted output like

// Columns (adjust the second number for positioning)
Console.WriteLine("{0,-30} = {1,-30}",property.Name,property.Value);
//Columns

//Comma delimited
int cnt = 0;
while(...)
{
Console.Write("{2}{0} = {1}",property.Name,property.Value,
(cnt==0)?"":",");
cnt++;
}

Or something more complex


Hope this helps
Bill
 
Top