简体   繁体   English

如何使用C#代码在IIS 7中删除或更新虚拟目录

[英]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; 我可以使用Microsoft.web.administration通过以下代码添加虚拟目录;

app.VirtualDirectories.Add("/vDir", "C:\\inetpub\\Ztet"); 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 app.virtualDirectories.Remove(?? ===失败

also Tried to overwrite with app.VirtualDirectories.Add("/vDir", "C:\\inetpub\\Xtet"); 还试图用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();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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