ManagementObject.Put() not persisting

D

David Clegg

I initially posted this to .Framework.WMI a week ago but am yet to have
a response...

I am having trouble changing properties of a Windows Service
application using the ManagementObject class (in particular the
StartMode property). I can change the property and calling
ManagementObject.Put doesn't throw any exceptions, but the setting
isn't changed. If I query the property after setting and putting, it
appears to be updated, but if you create a new ManagementObject
instance to retrieve the details again, the old property is returned.
What am I missing here? I have pasted some code below to demonstrate.

//***BEGIN CODE***
using System;
using System.Management;

namespace MOTest {
class MOTest {
static void Main(string[] args) {
string objectPath = "Win32_Service.Name='ebetCJMSvc'";
ManagementObject mo = new ManagementObject(objectPath);
if (mo != null) {
ShowStartMode(mo, "Before Put:");
if (mo["StartMode"].ToString() == "Manual")
mo["StartMode"] = "Auto";
else
mo["StartMode"] = "Manual";
ManagementPath mp = mo.Put();
ShowStartMode(mo, "After Put:");
ShowStartMode(objectPath, "After Reload:");

Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
}
}

static private void ShowStartMode(ManagementObject mo, string prefix)
{
if (mo != null) {
Console.WriteLine("{0} {1}", prefix, mo["StartMode"].ToString());
}
}

static private void ShowStartMode(string objectPath, string prefix) {
ManagementObject mo = new ManagementObject(objectPath);
ShowStartMode(mo, prefix);
}
}
}
//***END CODE***
 
S

Sam

I'm not 100% sure but you might have to do a Get() call
at the start to bind to the management object.

HTH

Sam
-----Original Message-----
I initially posted this to .Framework.WMI a week ago but am yet to have
a response...

I am having trouble changing properties of a Windows Service
application using the ManagementObject class (in particular the
StartMode property). I can change the property and calling
ManagementObject.Put doesn't throw any exceptions, but the setting
isn't changed. If I query the property after setting and putting, it
appears to be updated, but if you create a new ManagementObject
instance to retrieve the details again, the old property is returned.
What am I missing here? I have pasted some code below to demonstrate.

//***BEGIN CODE***
using System;
using System.Management;

namespace MOTest {
class MOTest {
static void Main(string[] args) {
string objectPath
= "Win32_Service.Name='ebetCJMSvc'";
ManagementObject mo = new ManagementObject(objectPath);
if (mo != null) {
ShowStartMode(mo, "Before Put:");
if (mo
["StartMode"].ToString() == "Manual")
mo["StartMode"] = "Auto";
else
mo["StartMode"] = "Manual";
ManagementPath mp = mo.Put ();
ShowStartMode(mo, "After Put:");
ShowStartMode (objectPath, "After Reload:");

Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
}
}

static private void ShowStartMode
(ManagementObject mo, string prefix)
{
if (mo != null) {
Console.WriteLine("{0}
{1}", prefix, mo["StartMode"].ToString());
 
S

Sam

Ignore my last post I was talking nonsense.

StartMode is a read only property, so you need to invoke
the ChangeStartMode method as in the following line of
code:

mo.InvokeMethod("ChangeStartMode", new object[]
{ "Automatic" });

Note that a) you don't need to do a Put(), b) You'll need
to do a Get() to update the current object and c) It
appears to use "Automatic" for the method call rather
than "Auto" which is the property value.

The following is the if statement from you're example
changed so it works...

if (mo["StartMode"].ToString() == "Manual")
mo.InvokeMethod("ChangeStartMode", new object[]
{ "Automatic" });
else
mo.InvokeMethod("ChangeStartMode", new object[]
{ "Manual" });
mo.Get();

HTH

Sam
-----Original Message-----
I'm not 100% sure but you might have to do a Get() call
at the start to bind to the management object.

HTH

Sam
-----Original Message-----
I initially posted this to .Framework.WMI a week ago
but
am yet to have
a response...

I am having trouble changing properties of a Windows Service
application using the ManagementObject class (in particular the
StartMode property). I can change the property and calling
ManagementObject.Put doesn't throw any exceptions, but the setting
isn't changed. If I query the property after setting
and
putting, it
appears to be updated, but if you create a new ManagementObject
instance to retrieve the details again, the old
property
is returned.
What am I missing here? I have pasted some code below
to
demonstrate.

//***BEGIN CODE***
using System;
using System.Management;

namespace MOTest {
class MOTest {
static void Main(string[] args) {
string objectPath
= "Win32_Service.Name='ebetCJMSvc'";
ManagementObject mo = new ManagementObject(objectPath);
if (mo != null) {
ShowStartMode(mo, "Before Put:");
if (mo
["StartMode"].ToString() == "Manual")
mo["StartMode"] = "Auto";
else
mo["StartMode"] = "Manual";
ManagementPath mp = mo.Put ();
ShowStartMode(mo, "After Put:");
ShowStartMode (objectPath, "After Reload:");

Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
}
}

static private void ShowStartMode
(ManagementObject mo, string prefix)
{
if (mo != null) {
Console.WriteLine("{0}
{1}", prefix, mo["StartMode"].ToString());
}
}

static private void ShowStartMode(string objectPath, string prefix) {
ManagementObject mo = new ManagementObject(objectPath);
ShowStartMode(mo, prefix);
}
}
}
//***END CODE***
.
.
 
D

David Clegg

Sam said:
Ignore my last post I was talking nonsense.

Done :)
StartMode is a read only property, so you need to invoke
the ChangeStartMode method as in the following line of
code:

mo.InvokeMethod("ChangeStartMode", new object[]
{ "Automatic" });

Note that a) you don't need to do a Put(), b) You'll need
to do a Get() to update the current object and c) It
appears to use "Automatic" for the method call rather
than "Auto" which is the property value.

Thanks Sam, I'll give that a go. BTW, is there a documented list of
which properties are read only, and need to be set that way? I would
have thought it would have been more helpful if the FCL had thrown an
exception if I attempted to set a read only property.
 
S

Sam

The Platform SDK contains all the properties of each WMI
class.

Follow this link for the Win32_Service class:

http://msdn.microsoft.com/library/en-
us/wmisdk/wmi/win32_service.asp

HTH

Sam
-----Original Message-----
Sam said:
Ignore my last post I was talking nonsense.

Done :)
StartMode is a read only property, so you need to invoke
the ChangeStartMode method as in the following line of
code:

mo.InvokeMethod("ChangeStartMode", new object[]
{ "Automatic" });

Note that a) you don't need to do a Put(), b) You'll need
to do a Get() to update the current object and c) It
appears to use "Automatic" for the method call rather
than "Auto" which is the property value.

Thanks Sam, I'll give that a go. BTW, is there a documented list of
which properties are read only, and need to be set that way? I would
have thought it would have been more helpful if the FCL had thrown an
exception if I attempted to set a read only property.
.
 
D

David Clegg

Thanks once again. Exactly what I was looking for.

--
Cheers,
David Clegg
dclegg_at_ebetonline_dot_com.

There's no place like 127.0.0.1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top