我是一名专业程序员,因此,我对我正在做的事情一无所知:)
严肃地说;不,我无论如何都不是专家。不过我确实有问题,不知道如何解决。好消息是,我(想我)知道问题出在哪里,我希望这里有人能提供帮助。
这是问题的概要。我正在用 C# 创建一个表单,它将为我完成一些服务器和数据库管理任务。我有一个按钮,单击该按钮应该返回“y”服务器上“x”服务的服务状态。状态在屏幕上打印到文本框。
这是我的代码:
private void button2_Click(object sender, EventArgs e)
{
string fs = "Service X Status = ";
string mr = "Service A Status = ";
string qp = "Service B Status = ";
string sp = "Spooler Service Status = ";
ServiceController fssc = new ServiceController("xService", "yServer");
ServiceController mrsc = new ServiceController("aService", "yServer");
ServiceController qpsc = new ServiceController("bService", "yServer");
ServiceController spsc = new ServiceController("Spooler", "yServer");
try
{
txtGtwySts.AppendText(sp + spsc.Status.ToString());
txtGtwySts.AppendText(Environment.NewLine);
txtGtwySts.AppendText(fs + fssc.Status.ToString());
txtGtwySts.AppendText(Environment.NewLine);
txtGtwySts.AppendText(mr + mrsc.Status.ToString());
txtGtwySts.AppendText(Environment.NewLine);
txtGtwySts.AppendText(qp + qpsc.Status.ToString());
}
catch (Exception crap)
{
string msg = "";
int i;
for (i = 0; i < crap.Message.Count(); i++)
{
msg += "Error # " + i + " Message: " + crap.Message + "\n";
}
MessageBox.Show(msg);
MessageBox.Show(i.ToString());
}
}
我得到例外,基本上是说:无法在“服务器”上打开“服务”。由于这是一个远程服务器,我假设这是一个凭证/安全问题。但是,我对 Spooler 服务没有任何问题。
我的问题是...如何将用户 ID 和密码传递给该服务器,以便它进行身份验证或运行,以便我可以检查这些服务的状态,这就是问题所在。如果有人不认为这是问题所在,请告诉我哪里出错了:)