1

I need to overwritte Default Site if isnt use, overwrite it if the user decides so or create a new one.

I am trying check all sites, and take this with port 80 and name "Default Web Site". Next if it exist I want to take physical path and check does it equal the default path and contains all files for default path. In this way:

    private static void CheckSite(SiteCollection applicationSites)
    {
        int i = 0;
        foreach (var item in applicationSites)
        {          
            foreach (var binding in item.Bindings)
            {
                int port;
                if (binding.EndPoint != null)
                    port = binding.EndPoint.Port;
                else
                    continue;
                if (port == 80 && item.Name == "Default Web Site")
                {
                     var path = item.Applications[i].VirtualDirectories[0].PhysicalPath;
                     string[] dire = Directory.GetFiles(path);
                }

            }
            i++;
        }
    }

Is there better way to do this? I am using Microsoft.Web.Administration.

Mat Rat
  • 105
  • 1
  • 11
  • You should filter out sites whose names do not match "Default Web Site" first, and then iterate bindings. That can save a lot of computation. – Lex Li May 25 '20 at 14:28
  • If there is no better way, but only possibly a shortening of calculations, they will stay with it, because in this way I check which ports are used. – Mat Rat May 26 '20 at 10:11
  • Do you want to get the Default Web Site? If yes, you only need to traverse serverManager.Sites as below code. – samwu May 26 '20 at 10:17

1 Answers1

0

You can get the Default Web Site as below code:

using (ServerManager serverManager = new ServerManager())
        {
            var sites = serverManager.Sites;
            foreach (Site site in sites)
            {
                var sitename = site.Name;
                if (sitename == "Default Web Site")
                {

                }                  
            }
        }
samwu
  • 3,857
  • 3
  • 11
  • 25
  • No I dont want do it I need to check if the content is default, if someone does not change it but left the name. – Mat Rat May 26 '20 at 10:49
  • What is content? In the above code, you get the port and name through multiple loops, which you can get through the more concise code – samwu May 29 '20 at 06:01
  • The question is how to check if the default site is the default one, not just find the "Default Web Site" – Mat Rat May 29 '20 at 07:06
  • You only need to know the name of the default site, and then judge whether the sitename is the same as the name of the default site. If the name is the same, it is the default site. – samwu May 29 '20 at 07:21
  • I will not agree because I have encountered cases that the content has been changed and the name defaul has been. Anyway, thanks for the answer, I will stay with my version. – Mat Rat May 29 '20 at 07:24