以编程方式解锁applicationHost.config中的部分

时间:2012-01-21 02:06:59

标签: c# .net iis

我已经检查了ServerManagaer类,它提供了许多与IIS一起工作的功能,它还包含更新applicationHost.config文件中的值的方法,但我无法解决那里的部分。

例如,为此目的,使用了appcmd.exe unlock config 命令。我需要以编程方式执行相同操作。

2 个答案:

答案 0 :(得分:3)

据我所知,您无法使用ServerManager执行锁定/解锁操作,但仍然可以以编程方式执行appcmd.exe以获得所需的结果:

System.Diagnostics.Process appCmdProc = new System.Diagnostics.Process();
appCmdProc.StartInfo.FileName = "Path-to-Directory\appcmd.exe";
appCmdProc.StartInfo.Arguments = "unlock config /section:sectionName";
appCmdProc.Start();

答案 1 :(得分:3)

如前所述,您可以运行appcmd进程。但只是暗示如果你不控制弹出窗口,你可以重定向输出。

以下是MSDN的代码

// Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit(); 

更多详细信息,请参阅HERE

相关问题