简体   繁体   中英

How to remove or update virtual directory in IIS 7 with C# code

I can add a virtual directory with the following code using Microsoft.web.administration;

app.VirtualDirectories.Add("/vDir", "C:\\inetpub\\Ztet");

and the code works but how can i update this or change this or delete this with code? lets say i wanted to change the virtual directory name or path?

I tried

app.virtualDirectories.Remove( ?? ===Fail

also Tried to overwrite with app.VirtualDirectories.Add("/vDir", "C:\\inetpub\\Xtet"); //with different path but fails once it already exists

Any tips will be appriciated also any idea on how to assign or change associated username and password for the virtual direcotry?

Here are a few samples:

        static void Main(string[] args)
    {
        CreateApp();

        RenameApp();

        EditApp();

        DeleteApp();
    }

    private static void EditApp()
    {
        using (ServerManager mgr = new ServerManager())
        {
            Application app = mgr.Sites["Default Web Site"].Applications["/TestAppNew"];
            VirtualDirectory vdir = app.VirtualDirectories["/"];
            vdir.UserName = "SomeUser";
            vdir.Password = "SomePassword";
            mgr.CommitChanges();
        }
    }

    private static void DeleteApp()
    {
        using (ServerManager mgr = new ServerManager())
        {

            Application app = mgr.Sites["Default Web Site"].Applications["/TestAppNew"];
            mgr.Sites["Default Web Site"].Applications.Remove(app);
            mgr.CommitChanges();
        }
    }

    private static void RenameApp()
    {
        using (ServerManager mgr = new ServerManager())
        {

            Application app = mgr.Sites["Default Web Site"].Applications["/TestApp"];
            app.Path = "/TestAppNew";

            mgr.CommitChanges();
        }
    }

    private static void CreateApp()
    {
        using (ServerManager mgr = new ServerManager())
        {

            mgr.Sites["Default Web Site"].Applications.Add("/TestApp", @"c:\inetpub\wwwroot");

            mgr.CommitChanges();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM