我这样写,在控制台应用程序中可以实现: Process p = new Process();
p.StartInfo.FileName="qq.exe";
p.Start();但是放到 windows服务 里,他不会弹出qq登录窗口,只在进程里有,也杀不死。不知道这样启动应用程序是否正确。
p.StartInfo.FileName="qq.exe";
p.Start();但是放到 windows服务 里,他不会弹出qq登录窗口,只在进程里有,也杀不死。不知道这样启动应用程序是否正确。
你可以写到注册表中呀.
HLM\software\microsoft\windows\currentversion\run下面呀.
在C#中好像有执行外部程序的方法,但我不记得了语句了。
{
string res = String.Empty;
string error = String.Empty;
Process p = new Process();
StreamWriter sw;
StreamReader sr;
//StreamReader err;
ProcessStartInfo psI = new ProcessStartInfo("cmd");
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.RedirectStandardError = true;
psI.CreateNoWindow = true;
p.StartInfo = psI; try
{
p.Start();
sw = p.StandardInput;
sr = p.StandardOutput;
//err = p.StandardError; sw.AutoFlush = true;
sw.WriteLine(cmd);
sw.Close();
//error = err.ReadToEnd();
res = sr.ReadToEnd();
//err.Close();
sr.Close();
p.Close();
}
catch(Exception e)
{
MessageBox.Show("Error when run command: " + cmd + "\n" + e.Message + "\n");
return "";
}
if(error.Length != 0)
{
MessageBox.Show("Error when run command: " + cmd + "\n" + error + "\n");
return "";
}
return res;
}其中的参数可以是QQ的路径。
我的代码在控制台应用程序中是没问题的,和你们的代码也一样。但是如何在windows服务中使用呢?
还是正如 BigIdiot628(大笨蛋,谁叫你不努力!)所说,服务并不能打开应用程序?
Process pro = new Process();
pro = Process.Start(@"文件的路径");
pro.WaitForExit();第二种方法:
Process tlbimp = new Process();
tlbimp.StartInfo.FileName = "tlbimp.exe";
tlbimp.StartInfo.CreateNoWindow = true;
tlbimp.StartInfo.RedirectStandardOutput = true;
tlbimp.StartInfo.UseShellExecute = false;
tlbimp.Start ();
tlbimp.WaitForExit();第三种方法:
首先引入:[DllImport("Shell32",CharSet=CharSet.Auto)]
public static extern Int32 ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd);然后再执行:ShellExecute(this.Handle,"open","aa.exe","","c:\\",5);
5 表示SW_SHOW,还有SW_HIDE等宏定义,在WinUser.h都有。自己去看看吧。
当然API函数还可以用WinExec(),ShellExecuteEx();