How do I do this in C#?

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I have the following vbscript that creates a WMI Class and sets the Key. How
would I do this in C#?

option explicit

Const wbemCimtypeString = 8

Dim oLocation, oServices, oInstances, oObject, oDataObject, oNewObject

Set oLocation = CreateObject("WbemScripting.SWbemLocator")

'Remove class'
on error resume next
Set oServices = oLocation.ConnectServer(, "root\cimv2")
set oNewObject = oServices.Get("TestCode")
oNewObject.Delete_
on error goto 0

'Create data class structure '
Set oServices = oLocation.ConnectServer(, "root\cimv2")
Set oDataObject = oServices.Get
oDataObject.Path_.Class = "TestCode"
oDataObject.Properties_.add "Name", wbemCimtypeString
oDataObject.Properties_.add "ScanTime", wbemCimtypeString
oDataObject.Properties_.add "Description", wbemCimtypeString
oDataObject.Properties_("Name").Qualifiers_.add "key", True

oDataObject.Put_

This is what I have so far:
static void Main(string[] args)
{
try
{
ManagementScope scopeDevice1 = new
ManagementScope("root\\CIMV2");
scopeDevice1.Options.EnablePrivileges = true; //sets
required privilege
scopeDevice1.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath1 = new
ManagementPath("TestCode");


ManagementClass printerClass1 = new
ManagementClass(scopeDevice1, managementPath1, null);
printerClass1.Properties.Add("Name", CimType.String,false);
printerClass1.Properties.Add("Description", CimType.String,
false);
printerClass1.Properties.Add("Scantime", CimType.String,
false);
//printerClass1.SetPropertyQualifierValue("Name","read",
"here i come to save the day");
printerClass1.Put();

}
catch (Exception ex)
{
//throw new Exception( String.Format( "WMI exception: {0}",
ex.Message));
Console.WriteLine(String.Format("Create Class: {0}",
ex.Message));
}

try
{
ManagementScope scopeDevice = new
ManagementScope("root\\CIMV2");
scopeDevice.Options.EnablePrivileges = true; //sets required
privilege
scopeDevice.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath = new
ManagementPath("TestCode");
ManagementClass printerClass = new
ManagementClass(scopeDevice, managementPath, null);
ManagementObject printerObject =
printerClass.CreateInstance();
printerObject["Name"] = "Test";
printerObject["Description"] = "I want this to work";
printerObject["Scantime"] = "This is the time";
PutOptions options = new PutOptions();
options.Type = PutType.UpdateOrCreate;
printerObject.Put(options);
}
catch ( Exception ex)
{
//throw new Exception( String.Format( "WMI exception: {0}",
ex.Message));
Console.WriteLine ( String.Format( "Add Data: {0}",
ex.Message));
}

Console.ReadLine();
}


If the class exists, then the properties are set, but no data added. If I
manually set the key, then the data is added.

Thanks.
 
there exist some vbnet->csharp comercial code converters, try google for it.

hope this helps, Carlos.


Tom said:
I have the following vbscript that creates a WMI Class and sets the Key.
How
would I do this in C#?

option explicit

Const wbemCimtypeString = 8

Dim oLocation, oServices, oInstances, oObject, oDataObject, oNewObject

Set oLocation = CreateObject("WbemScripting.SWbemLocator")

'Remove class'
on error resume next
Set oServices = oLocation.ConnectServer(, "root\cimv2")
set oNewObject = oServices.Get("TestCode")
oNewObject.Delete_
on error goto 0

'Create data class structure '
Set oServices = oLocation.ConnectServer(, "root\cimv2")
Set oDataObject = oServices.Get
oDataObject.Path_.Class = "TestCode"
oDataObject.Properties_.add "Name", wbemCimtypeString
oDataObject.Properties_.add "ScanTime", wbemCimtypeString
oDataObject.Properties_.add "Description", wbemCimtypeString
oDataObject.Properties_("Name").Qualifiers_.add "key", True

oDataObject.Put_

This is what I have so far:
static void Main(string[] args)
{
try
{
ManagementScope scopeDevice1 = new
ManagementScope("root\\CIMV2");
scopeDevice1.Options.EnablePrivileges = true; //sets
required privilege
scopeDevice1.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath1 = new
ManagementPath("TestCode");


ManagementClass printerClass1 = new
ManagementClass(scopeDevice1, managementPath1, null);
printerClass1.Properties.Add("Name", CimType.String,false);
printerClass1.Properties.Add("Description", CimType.String,
false);
printerClass1.Properties.Add("Scantime", CimType.String,
false);
//printerClass1.SetPropertyQualifierValue("Name","read",
"here i come to save the day");
printerClass1.Put();

}
catch (Exception ex)
{
//throw new Exception( String.Format( "WMI exception:
{0}",
ex.Message));
Console.WriteLine(String.Format("Create Class: {0}",
ex.Message));
}

try
{
ManagementScope scopeDevice = new
ManagementScope("root\\CIMV2");
scopeDevice.Options.EnablePrivileges = true; //sets
required
privilege
scopeDevice.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath = new
ManagementPath("TestCode");
ManagementClass printerClass = new
ManagementClass(scopeDevice, managementPath, null);
ManagementObject printerObject =
printerClass.CreateInstance();
printerObject["Name"] = "Test";
printerObject["Description"] = "I want this to work";
printerObject["Scantime"] = "This is the time";
PutOptions options = new PutOptions();
options.Type = PutType.UpdateOrCreate;
printerObject.Put(options);
}
catch ( Exception ex)
{
//throw new Exception( String.Format( "WMI exception: {0}",
ex.Message));
Console.WriteLine ( String.Format( "Add Data: {0}",
ex.Message));
}

Console.ReadLine();
}


If the class exists, then the properties are set, but no data added. If I
manually set the key, then the data is added.

Thanks.
 
VBnet-> c# won't help him because this is vbscript.

There are some native classes for working with WMI in the .NET framework.
The question I have is do you know how to program in C# and you just want
pointers to a similar API? or do you want the code translated for you. If
you want the API, you need to look at the System.Management.Instrumentation
namespace. If you need the code translated, I can look at it over my lunch
break for you.



xcal said:
there exist some vbnet->csharp comercial code converters, try google for
it.

hope this helps, Carlos.


Tom said:
I have the following vbscript that creates a WMI Class and sets the Key.
How
would I do this in C#?

option explicit

Const wbemCimtypeString = 8

Dim oLocation, oServices, oInstances, oObject, oDataObject, oNewObject

Set oLocation = CreateObject("WbemScripting.SWbemLocator")

'Remove class'
on error resume next
Set oServices = oLocation.ConnectServer(, "root\cimv2")
set oNewObject = oServices.Get("TestCode")
oNewObject.Delete_
on error goto 0

'Create data class structure '
Set oServices = oLocation.ConnectServer(, "root\cimv2")
Set oDataObject = oServices.Get
oDataObject.Path_.Class = "TestCode"
oDataObject.Properties_.add "Name", wbemCimtypeString
oDataObject.Properties_.add "ScanTime", wbemCimtypeString
oDataObject.Properties_.add "Description", wbemCimtypeString
oDataObject.Properties_("Name").Qualifiers_.add "key", True

oDataObject.Put_

This is what I have so far:
static void Main(string[] args)
{
try
{
ManagementScope scopeDevice1 = new
ManagementScope("root\\CIMV2");
scopeDevice1.Options.EnablePrivileges = true; //sets
required privilege
scopeDevice1.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath1 = new
ManagementPath("TestCode");


ManagementClass printerClass1 = new
ManagementClass(scopeDevice1, managementPath1, null);
printerClass1.Properties.Add("Name",
CimType.String,false);
printerClass1.Properties.Add("Description",
CimType.String,
false);
printerClass1.Properties.Add("Scantime", CimType.String,
false);
//printerClass1.SetPropertyQualifierValue("Name","read",
"here i come to save the day");
printerClass1.Put();

}
catch (Exception ex)
{
//throw new Exception( String.Format( "WMI exception:
{0}",
ex.Message));
Console.WriteLine(String.Format("Create Class: {0}",
ex.Message));
}

try
{
ManagementScope scopeDevice = new
ManagementScope("root\\CIMV2");
scopeDevice.Options.EnablePrivileges = true; //sets
required
privilege
scopeDevice.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath = new
ManagementPath("TestCode");
ManagementClass printerClass = new
ManagementClass(scopeDevice, managementPath, null);
ManagementObject printerObject =
printerClass.CreateInstance();
printerObject["Name"] = "Test";
printerObject["Description"] = "I want this to work";
printerObject["Scantime"] = "This is the time";
PutOptions options = new PutOptions();
options.Type = PutType.UpdateOrCreate;
printerObject.Put(options);
}
catch ( Exception ex)
{
//throw new Exception( String.Format( "WMI exception: {0}",
ex.Message));
Console.WriteLine ( String.Format( "Add Data: {0}",
ex.Message));
}

Console.ReadLine();
}


If the class exists, then the properties are set, but no data added. If
I
manually set the key, then the data is added.

Thanks.
 
it is easy to write vb or vbnet code from vbs, if he have some problem with
this,
I think he must ask the forum only at that point, not about code conversion
issues

Carlos.


Mike Brown said:
VBnet-> c# won't help him because this is vbscript.

There are some native classes for working with WMI in the .NET framework.
The question I have is do you know how to program in C# and you just want
pointers to a similar API? or do you want the code translated for you. If
you want the API, you need to look at the
System.Management.Instrumentation namespace. If you need the code
translated, I can look at it over my lunch break for you.



xcal said:
there exist some vbnet->csharp comercial code converters, try google for
it.

hope this helps, Carlos.


Tom said:
I have the following vbscript that creates a WMI Class and sets the Key.
How
would I do this in C#?

option explicit

Const wbemCimtypeString = 8

Dim oLocation, oServices, oInstances, oObject, oDataObject, oNewObject

Set oLocation = CreateObject("WbemScripting.SWbemLocator")

'Remove class'
on error resume next
Set oServices = oLocation.ConnectServer(, "root\cimv2")
set oNewObject = oServices.Get("TestCode")
oNewObject.Delete_
on error goto 0

'Create data class structure '
Set oServices = oLocation.ConnectServer(, "root\cimv2")
Set oDataObject = oServices.Get
oDataObject.Path_.Class = "TestCode"
oDataObject.Properties_.add "Name", wbemCimtypeString
oDataObject.Properties_.add "ScanTime", wbemCimtypeString
oDataObject.Properties_.add "Description", wbemCimtypeString
oDataObject.Properties_("Name").Qualifiers_.add "key", True

oDataObject.Put_

This is what I have so far:
static void Main(string[] args)
{
try
{
ManagementScope scopeDevice1 = new
ManagementScope("root\\CIMV2");
scopeDevice1.Options.EnablePrivileges = true; //sets
required privilege
scopeDevice1.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath1 = new
ManagementPath("TestCode");


ManagementClass printerClass1 = new
ManagementClass(scopeDevice1, managementPath1, null);
printerClass1.Properties.Add("Name",
CimType.String,false);
printerClass1.Properties.Add("Description",
CimType.String,
false);
printerClass1.Properties.Add("Scantime", CimType.String,
false);
//printerClass1.SetPropertyQualifierValue("Name","read",
"here i come to save the day");
printerClass1.Put();

}
catch (Exception ex)
{
//throw new Exception( String.Format( "WMI exception:
{0}",
ex.Message));
Console.WriteLine(String.Format("Create Class: {0}",
ex.Message));
}

try
{
ManagementScope scopeDevice = new
ManagementScope("root\\CIMV2");
scopeDevice.Options.EnablePrivileges = true; //sets
required
privilege
scopeDevice.Options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementPath managementPath = new
ManagementPath("TestCode");
ManagementClass printerClass = new
ManagementClass(scopeDevice, managementPath, null);
ManagementObject printerObject =
printerClass.CreateInstance();
printerObject["Name"] = "Test";
printerObject["Description"] = "I want this to work";
printerObject["Scantime"] = "This is the time";
PutOptions options = new PutOptions();
options.Type = PutType.UpdateOrCreate;
printerObject.Put(options);
}
catch ( Exception ex)
{
//throw new Exception( String.Format( "WMI exception: {0}",
ex.Message));
Console.WriteLine ( String.Format( "Add Data: {0}",
ex.Message));
}

Console.ReadLine();
}


If the class exists, then the properties are set, but no data added. If
I
manually set the key, then the data is added.

Thanks.
 
Back
Top