C# 在C中停止IIS#
我正在使用C#自动化一些过程C# 在C中停止IIS#,c#,multithreading,iis,C#,Multithreading,Iis,我正在使用C#自动化一些过程 启动IIS 在特定端口的特定地址打开Internet Explorer 用户关闭浏览器 停止IIS 我被困在第四步,因为我必须使用相同的端口进行其他程序后 我创建了一个将被调用以启动IIS的线程: public class MyThread { //thread to start IIS public static void Thread1() { //thread for running IIS using
public class MyThread
{
//thread to start IIS
public static void Thread1()
{
//thread for running IIS
using (Process proc = new Process())
{
proc.StartInfo.FileName = @"C:\Program Files\IIS Express\iisexpress.exe";
proc.StartInfo.Arguments = @"/path:""c:\windows\Microsoft.NET\Framework\v4.0.30319\ASP.NETWebAdminFiles"" /vpath:/asp.netwebadminfiles /port:61569 /clr:4.0 /ntlm";
//proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.WaitForExit();
//output from the process run
Console.Out.WriteLine(proc.StandardOutput.ReadToEnd());
}
}
}
打开IE的代码是:
Thread thread1 = new Thread(new ThreadStart(MyThread.Thread1));
thread1.Start();
using (Process proc1 = new Process())
{
proc1.StartInfo.FileName = @"C:\Program Files\Internet Explorer\iexplore.exe";
proc1.StartInfo.Arguments = @"http://localhost:61569/asp.netwebadminfiles/default.aspx?applicationPhysicalPath=C:\Users\"+userName+@"\Documents\Visual Studio 2013\Projects\NN\&applicationUrl=/";
proc1.StartInfo.UseShellExecute = false;
proc1.StartInfo.RedirectStandardOutput = true;
proc1.StartInfo.CreateNoWindow = true;
proc1.Start();
proc1.WaitForExit();
//output from the process run
Console.Out.WriteLine(proc1.StandardOutput.ReadToEnd());
}
我试图创建另一个线程来终止/中止原始线程,但这不起作用。我还尝试在控制台的末尾写“Q”,但这也没用
你知道如何在浏览器关闭时停止IIS吗?你不应该停止IIS的进程。把地毯拉到下面不是一件好事情,而且,如果服务有任何重启动作,那么它将立即重新启动 使用ServiceController()启动和停止相应的服务
还有其他控制IIS的方法,例如使用WMI(我认为现在不推荐使用WMI),但是使用ServiceController应该足以完成您的任务。不确定您遇到的问题是什么;IE关闭时,
WaitForExit
是否正确返回?我可以问一下你的IISExpress是做什么的吗?有可能有一种更直接的方法来执行您正在尝试的操作。您是否看到假设您的应用程序以提升的权限级别运行您是否考虑过从您的代码中执行此命令行net stop w3svc/Y
?请尝试阅读本文:iisreset/stop
或appcmd stop site“sitename”
(您没有说明IIS的哪个版本,后者在IIS 7+中更受欢迎。