我正在尝试使用web api,它允许我在IIS中启动/停止/重置我的应用程序池和站点。
目前,我已经测试过运行管理级别Visual Studio允许我完美地运行我的代码。但是,一旦我没有运行具有特权管理级别的visual studio,或者甚至部署到IIS并作为测试服务运行,我的方法内部就会中断。
关于它是如何完成的一点概述我得到一个运行CMD的进程,它使用appcmd列出并重置iis连接。
private List<SiteModels> GetSiteModels()
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Minimized,
FileName = "cmd.exe",
WorkingDirectory = @"C:\",
Arguments = "/K appcmd list sites & exit",
Verb = "runas",
RedirectStandardOutput = true
};
process.StartInfo = startInfo;
process.EnableRaisingEvents = false;
List<SiteModels> sitesModelList = new List<SiteModels>();
string tempString = process.StandardOutput.ReadToEnd();
process.WaitForExit();
foreach (var line in tempString.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
SiteModels sitesModel = new SiteModels();
string pattern = "SITE \"(?<SiteName>.*?)\"[\\s\\S]*?id:(?<Id>.*?),[\\s\\S]*?bindings:(?<Bindings>.*?),[\\s\\S]*?state:(?<State>.*?)\\)";
Regex regexPattern = new Regex(pattern);
Match match = regexPattern.Match(line);
if (match.Success)
{
sitesModel.SiteName = match.Groups["SiteName"].Value.Trim();
sitesModel.Id = int.Parse(match.Groups["Id"].Value.Trim());
sitesModel.Bindings = match.Groups["Bindings"].Value.Trim();
sitesModel.State = match.Groups["State"].Value.Trim();
sitesModelList.Add(sitesModel);
}
}
return sitesModelList;
}