简体   繁体   中英

How can i get the Physical path of a site in IIS using c#?

I need to remove a site when the user uninstalls an application. I have no idea of the site name, but the Physical path will always be the same. So is there a way i can search throgh the returned list of sites and check if the particular physical path is a certain value, I will know that that is the site and from that get the site name to delete. Would the app pool for the site be in taht object?

I am using the following to get the websites,

        var iisManager = new ServerManager();
        SiteCollection sites = iisManager.Sites;

Or is there a way to get the "Advanced Settinsg" of a site?

After we added the Microsoft.Web.Administration.dll assembly file to the project, please refer to the below code snippets.

var iismanager = new ServerManager();
            SiteCollection sites = iismanager.Sites;
            foreach (var site in sites)
            {
                Console.WriteLine($"WebSite Name: {site.Name}");

                ObjectState siteState = site.State;
                Console.WriteLine($"Running State: {siteState}");

                ApplicationCollection applications = site.Applications;
                foreach (Microsoft.Web.Administration.Application item in applications)
                {
                    string applicationPoolName = item.ApplicationPoolName;
                    Console.WriteLine($"ApplicationPoolName: {applicationPoolName}");
                    VirtualDirectoryCollection directories = item.VirtualDirectories;
                    foreach (var directory in directories)
                    {
                        Console.WriteLine($"PhysicalPath: {directory.PhysicalPath}");
                    }
                }
                Console.WriteLine("-------------------------------");
            }

In addition, here is a detailed usage of the IIS Manager.
https://johnlnelson.com/2014/06/15/the-microsoft-web-administration-namespace/

Microsoft.Web.Administration dll allows us to perform the IIS administrative tasks. It contains a series of convenient top-level objects, such as sites, applications, application pools, application domains, virtual directories, and worker processes. We can use the API to obtain and work with the configuration and state of these objects and perform such actions as creating a site, starting or stopping a site, deleting an application pool, recycling an application pool, end even unloading application domains.
https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.administration?view=iis-dotnet

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