0

I'm trying to add some custom properties to the configuration of a virtual directory in IIS but when I'm committing the changes I get a DirectoryNotFoundException

I've tried multiple ways of doing it, most something like this

var config = serverManager.GetWebConfiguration(serverManager.Sites[0].Name, "mycustomfolder");
var appSettings = config .GetSection("appSettings");
var collection = section.GetCollection();
var elem = conf.CreateElement("add");
elem.SetAttributeValue("key", "createdBy");
elem.SetAttributeValue("value", "me");

serverManager.CommitChanges();

The server manager object is clearly defined with

using(var serverManager = new ServerManager())
...

What I was expecting would be something like this inside the configuration:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="true" />
    </system.webServer>
    <appSettings>
        <clear />
        <add key="createdBy" value="me" />
    </appSettings>
</configuration>

Instead I am getting an error

System.IO.DirectoryNotFoundException: Filename: \\?\C:\inetpub\wwwroot\local\web.config
Error: Cannot write configuration file

   at Microsoft.Web.Administration.Interop.IAppHostWritableAdminManager.CommitChanges()
   at Microsoft.Web.Administration.ConfigurationManager.CommitChanges()
   at Microsoft.Web.Administration.ServerManager.CommitChanges()

Following the path in which it is trying to save I cannot see subfolders for my virtual directories, but I can create them, even programmatically

AzeExMachina
  • 104
  • 14
  • A client connecting to a IIS server only has Guest Privileges so a connection cannot read the folders on the server. The application would need to "run as admin" to have access to the folders. – jdweng Jun 20 '19 at 14:22
  • The application using this portion of the code has the privileges needed. I am also doing other operations like adding new virtual directories or mime types. If I comment out the last three lines, those about the element creation I can do all of the tasks described. I'm also trying this with Visual Studio as an admin – AzeExMachina Jun 20 '19 at 14:40
  • Did you look at the XML file on following webpage : https://learn.microsoft.com/en-us/iis/manage/scripting/how-to-use-microsoftwebadministration – jdweng Jun 20 '19 at 15:09
  • Yes, that's what I've used as reference for my code – AzeExMachina Jun 20 '19 at 16:03

1 Answers1

0

According to your description, I guess you may used the wrong site name. I have also created a test demo on my side, it works well. I suggest you could try to use site name directly.

Details, you could refer to below codes:

    protected void Button1_Click(object sender, EventArgs e)
    {
        using (var serverManager = new ServerManager())
        {
            var config = serverManager.GetWebConfiguration("WebForm48", "mycustomfolder");
            var appSettings = config.GetSection("appSettings");
            var collection = appSettings.GetCollection();
            var elem = collection.CreateElement("add");
            elem.SetAttributeValue("key", "createdBy");
            elem.SetAttributeValue("value", "me");
            collection.Add(elem);
            serverManager.CommitChanges();
        }        
    }

My IIS console:

enter image description here

Result:

enter image description here

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65