2

I have checked ServerManagaer class and it gives a lot of functionality to work with IIS, it also contains methods to update values in applicationHost.config file, but I can't fine any way to unlock sections there.

For example for that purpose appcmd.exe unlock config command is used. I need to do the same programmatically.

NDeveloper
  • 1,837
  • 4
  • 20
  • 34
  • see [programmatically-unlocking-iis-configuration-sections-in-powershell](https://stackoverflow.com/questions/5717154/programmatically-unlocking-iis-configuration-sections-in-powershell) You can also do that using c# – Dream Jan 12 '18 at 04:28

2 Answers2

3

To my knowledge you can't perform lock/unlock action using ServerManager but still you can execute appcmd.exe programatically to achieve the desired result:

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();
Waqas
  • 6,812
  • 2
  • 33
  • 50
3

As already said you can run appcmd process. But just a hint that if you don't console to popup you can redirect the output.

Here is the code from 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(); 

More details see HERE

Incognito
  • 16,567
  • 9
  • 52
  • 74