title: appcmd.exe Cannot read configuration file due to insufficient permissions
body: <p>I have followed the steps at <a href="https://stackoverflow.com/questions/5615296/cannot-read-configuration-file-due-to-insufficient-permissions">Cannot read configuration file due to insufficient permissions</a></p>
  <p>I also tried to manually set the rights to related files, but that didn't work, especially on <code>appcmd.exe</code>.</p>
  <p>I am trying to update <code>applicationHost.config</code> to set IIS reset time dynamically, when my website loads on IIS. For this I am trying to execute the following code in <code>global.asax</code> file of my project:</p>
  <pre><code>string WindowsDir = Server.MapPath(&quot;~/Startup&quot;);
  string command = WindowsDir + @&quot;\Startup.cmd&quot;;
  string outputFilePath = WindowsDir + @&quot;\log.txt&quot;;
  string arguments = String.Format(
      &quot;/c echo Startup task (Startup.cmd) executed at {0} &gt;&gt;\&quot;{1}\&quot;&quot;, 
      System.DateTime.UtcNow.ToString(),
      outputFilePath);
  // System.Diagnostics.Process.Start(command, arguments);
  
  System.Diagnostics.Process process = new System.Diagnostics.Process();
  System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  startInfo.FileName = command;
  startInfo.Arguments = arguments;
  
  process.StartInfo = startInfo;
  process.Start();
  </code></pre>
  <p>This works perfectly when I run my website in Visual Studio, but when I deploy it on IIS, it produces the following errors:</p>
  <blockquote>
  <p>Prevent the IIS app pools from shutting down due to being idle:</p>
  <p><code>appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00 /commit:apphost</code></p>
  <pre><code>ERROR ( message:Configuration error 
  Filename: redirection.config
  Line Number: 0
  Description: Cannot read configuration file due to insufficient permissions. )
  </code></pre>
  <p>Schedule IIS app pool recycles for 8:00 AM UTC time (1:00 AM MST):</p>
  <p><code>appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.schedule.[value='08:00:00']&quot; /commit:apphost</code></p>
  <pre><code>ERROR ( message:Configuration error Filename: redirection.config
  Line Number: 0
  Description: Cannot read configuration file due to insufficient permissions. )
  </code></pre>
  <p>Prevent IIS app pool recycles from recycling on the default schedule of 1740 minutes (29 hours):</p>
  <p><code>appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.time:00:00:00 /commit:apphost</code></p>
  <pre><code>ERROR ( message:Configuration error 
  Filename: redirection.config
  Line Number: 0
  Description: Cannot read configuration file due to insufficient permissions. )
  </code></pre>
  </blockquote>
  <p>Then I tried to execute following code:</p>
  <pre><code>string WindowsDir = Server.MapPath(&quot;~/Startup&quot;);
  string command = WindowsDir + @&quot;\Startup.cmd&quot;;
  string outputFilePath = WindowsDir + @&quot;\log.txt&quot;;
  string arguments = String.Format(
      &quot;/c echo Startup task (Startup.cmd) executed at {0} &gt;&gt;\&quot;{1}\&quot;&quot;, 
      System.DateTime.UtcNow.ToString(),
      outputFilePath);
  // System.Diagnostics.Process.Start(command, arguments);
  
  var process = new System.Diagnostics.Process();
  var startInfo = new System.Diagnostics.ProcessStartInfo();
  startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  startInfo.FileName = command;
  startInfo.Arguments = arguments;
  
  string name = &quot;Administrator&quot;;
  string pass = &quot;password&quot;;
  
  startInfo.Password = pass.Aggregate(new System.Security.SecureString(), (ss, c) =&gt; { ss.AppendChar(c); return ss; });
  startInfo.UserName = name;
  startInfo.UseShellExecute = false;
  startInfo.RedirectStandardOutput = true;
  
  process.StartInfo = startInfo;
  process.Start();
  </code></pre>
  <p>Then it didn't work even from Visual Studio. I then reverted back to code that was working in Visual Studio (as mentioned above). And added the following tags in <code>Web.config</code> (inside <code>&lt;system.web&gt;</code> tag):</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;authentication mode=&quot;Windows&quot;/&gt;
  &lt;identity impersonate=&quot;true&quot; userName=&quot;Administrator&quot; password=&quot;password&quot;/&gt;
  </code></pre>
  <p>But that didn't work when I ran the website on IIS (7.5). Any ideas how to make this work?</p>