|
This is a 2 part question. I have a service that has a name "Ads 1", "Ads 2". They take a parameter of "1" and "2".
1. When I invoke the method to start the service "Ads 1" the " 1" is truncated, so I get "Ads" being passed. How can I stop this and force it to be "Ads 1"? All my other services start, stop, pause, everything else just fine, but they don't have a space then number either. What am I missing?
2. I am trying to pass the parameter with the startservice. I am using a hardcoded parameter value(1) for now to get it to work in testing. Due to the fact that I am unable to get the service to start I am not sure the parameter is being passed. How can I check to see if the paramter is being passed and/or Am I doing this right way?
Any help is appreciated. Thank you.
<pre lang="c#">
int iPos;
int iCount = 0;
string sService;
try
{
ConnectionOptions conOpts = new ConnectionOptions();
conOpts.Username = ddlServers.SelectedItem.Text.ToString() + "\\" + System.Configuration.ConfigurationManager.AppSettings["AdminUser"].ToString();
conOpts.Password = System.Configuration.ConfigurationManager.AppSettings["AdminPassword"].ToString();
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\" +
ddlServers.SelectedItem.Text +
"\\root\\cimv2", conOpts);
while (iCount < lboxServices.Items.Count)
{
if (lboxServices.Items[iCount].Selected)
{
iPos = lboxServices.Items[iCount].Text.IndexOf("-");
sService = lboxServices.Items[iCount].Text.Substring(0, iPos).Trim();
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select * from Win32_Service Where Name='" +
sService +
"'");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
if (oReturnCollection.Count > 0)
{
foreach (ManagementObject oReturn in oReturnCollection)
{
if (oReturn.GetPropertyValue("State").ToString().ToLower().Equals("stopped") ||
oReturn.GetPropertyValue("State").ToString().ToLower().Equals("paused"))
{
String[] sArgs = new string[] { "1" };
oReturn.InvokeMethod("StartService", sArgs);
}
}
}
lboxServices.Items[iCount].Text = sService + " - " + CheckService(ddlServers.SelectedItem.Text, sService);
}
iCount++;
}
}
catch (Exception err)
{
lboxServices.Items.Add("Error: " + err.Message.ToString());
}
|
|
|
|