-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: SitaramanM
Message 6 in Discussion

Hi Atlast, figured it out.  Displaying the values is fine with properties of the 
management object. But for setting i need to use the InvokeMethod function.  Funny!!!  
 The articles  were of a GREAT help.  The C# code is as below   spark : one question 
for u at the end of the post    using System;
using System.Management;
using System.ServiceProcess;
using System.Reflection;
namespace CSharp
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 class Class1
 {
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   Class1 x=new Class1();
   Console.WriteLine("Name of Service and StartMode before Changing...");
            x.DisplayData();
   Console.WriteLine("Changing StartMode ...");
   x.ChangeData();
   Console.WriteLine("Name of Service and StartMode after Changing...");
   x.DisplayData();
   Console.ReadLine();
  }
  
  void DisplayData()
  {
   //
   // TODO: Add code to start application here
   //
   ManagementClass mc = new ManagementClass("Win32_Service");
   ManagementObjectCollection moc = mc.GetInstances();
   foreach(ManagementObject mo in moc)
   {
    try
    {
     if (mo.Properties["Name"].Value.ToString()=="WINS")
     {
      Console.WriteLine(mo.Properties["Name"].Value.ToString() + ":" + 
mo.Properties["StartMode"].Value.ToString());
     }
     
    }
    catch(Exception p_objException)
    {
     Console.WriteLine(p_objException.Message);
    }
   }
  }   void ChangeData()
  {
   //
   // TODO: Add code to start application here
   //
   ManagementClass mc = new ManagementClass("Win32_Service");
   ManagementObjectCollection moc = mc.GetInstances();
   foreach(ManagementObject mo in moc)
   {
    try
    {
     if (mo.Properties["Name"].Value.ToString()=="WINS")
     {
      //The following lines DONT work???????
      mo.Properties["StartMode"].Value="Manual";
      mo.SetPropertyValue("StartMode","Manual");
      //The above lines DONT work???????         //The following lines WORK!!!!!!
      Object[] o1=new object[1];
      o1[0]="Automatic"; // Set the startmode as "Automatic"(Not "Auto" though the WMI 
Classes print it as "Auto") or "Manual" or "Disabled"
      mo.InvokeMethod("ChangeStartMode",o1);
      //The above lines WORK!!!!!!
     }
    }
    catch(Exception p_objException)
    {
     Console.WriteLine(p_objException.Message);
    }
    
   }
  }
 }
}
       spark : though the invokeMethod works and the job gets done, an academic 
question. why doesnt the changes get reflected when i just change the properties.  my 
guess is that it is disconnected(!!!!!!).  if so then, how do i save it back to the 
management object???   hth   regards,   sr

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to