做了个按钮,点击之后启动一个服务,我试过用servicecontroller类,和process类,甚至创建了一个bat文件启动,都是一样啊,在我本机上测试时好用,安到别人机器上之后就不好用了,到底是哪出问题了啊,各位高人帮忙~~服务是我自己写的,跟这有关系吗?
解决方案 »
- 进度条的数值怎么实时变化?
- 请问为什么要使用序列化?
- C# 含有静态成员的类 该如何序列化为XML及反序列化
- 在web怎么查看服务器的文件,比如word,excel,pdf,但只能看到内容不可以下载。或者保存到本地。请问这样有没有什么方法啊。
- 关于网页源码获取汉字乱码的问题
- C# 操作SQL如何达到最佳方案 老手请进,问题解决高分回报
- 用字节数组表示的ASCII怎么转化为数字,中文或者英文,高分求教,急!
- DLL函数调用出错:未将对象引用设置到对象的实例
- 远程机器上完全共享个文件夹
- .NET可以涵盖COM所有得内容吗?
- C#怎么给IE传URL并让当前IE打开目标网页
- 新手询问一个Datagridview的问题 望不吝赐教
http://blog.csdn.net/zhzuo/archive/2004/03/29/22025.aspx
{
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, sc.MachineName, "AutoReport");
scp.Demand();
sc.Start();
//如果服务20秒后还没有启动成功,报告失败
sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
}
catch (Exception err)
{
Label1.Text = "启动自动上报服务失败,请重新尝试";
Label1.Visible = true;
{
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, sc.MachineName, "AutoReport");
scp.Demand();
//sc.Start();
Process p = new Process();
p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; // not display window
p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine("net start AutoReport "); p.StandardInput.WriteLine("exit"); //string strRst = p.StandardOutput.ReadToEnd();
p.Close();
//如果服务20秒后还没有启动成功,报告失败
sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
}
catch (Exception err)
{
Label1.Text = "启动自动上报服务失败,请重新尝试";
Label1.Visible = true;
{
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, sc.MachineName, "AutoReport");
scp.Demand();
//sc.Start();
System.IO.FileStream fs = System.IO.File.Create(Request.PhysicalApplicationPath + "report.bat");
byte[] bs = new byte[20];
string s = "net start AutoReport";
for (int i = 0; i < s.Length; i++)
{
bs[i] = (byte)s[i];
}
fs.Write(bs,0,bs.Length);
fs.Close();
Process p = new Process();
p.StartInfo.FileName = Request.PhysicalApplicationPath + "report.bat";
p.StartInfo.UseShellExecute = false;
//Response.Write(p.StartInfo.UseShellExecute.ToString()); p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; // not display window
p.StartInfo.CreateNoWindow = true; p.Start(); //p.StandardInput.WriteLine("net start AutoReport "); //p.StandardInput.WriteLine("exit"); //string strRst = p.StandardOutput.ReadToEnd();
p.Close();
//如果服务20秒后还没有启动成功,报告失败
sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
}
catch (Exception err)
{
Label1.Text = "启动自动上报服务失败,请重新尝试";
Label1.Visible = true;
Visual Studio 6.0 版呀
http://blog.csdn.net/zhzuo/archive/2004/03/29/22025.aspx
windows service的用户密码名改了没,写在bat里面的。
让我们看看那行代码。
ServiceController sc = new ServiceController("AutoReport");
你说的密码是指什么啊,我生成的bat文件里就一行 net start AutoReport这个服务程序我在制作的时候account给的是local system啊。
scp.Demand();
”
这些注释掉试试!
我原来就是没有这些的,后来我研究了半天以为是权限的问题,就后加上的。结果如前啊
如果我用手动启动的话,几秒的时间,和别的服务一样。但我发现我运行别人机器上时点了按钮就反应挺慢了,下边进度条走半天,不过好像也没超过这句话里设置的时间sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
如果没问题的话,那我也不知道怎么回事了!
如果有问题的话,那就是用户权限的问题了!
解决办法:1将服务设置为自启动;
2在用户手册上安装方法中标明“第一次安装后需要在服务管理器中手工启动服务”问题解决~
再次感谢大家的帮助~~~~~~~~结帖~~~~~~~~