2009-03-17 5 views

Antwort

2

bereits. Sie müssen auf die COM-Komponente "AppHostAdminLibrary" verweisen

using AppHostAdminLibrary; 

... 

public void AddFtp7Site(String siteName, String siteId, String siteRoot) { 
    String configPath; 
    String configSectionName; 
    var fNewSite = false; 
    var fNewApplication = false; 
    var fNewVDir = false; 
    // 
    // First setup the sites section 
    // 
    configPath = "MACHINE/WEBROOT/APPHOST"; 
    configSectionName = "system.applicationHost/sites"; 

    var adminManager = new AppHostAdminLibrary.AppHostWritableAdminManager(); 
    adminManager.CommitPath = configPath; 

    try { 
     var sitesElement = adminManager.GetAdminSection(configSectionName, configPath); 
     IAppHostElement newSiteElement = null; 

     // 
     // check if site already exists 
     // 
     for (var i = 0; i < sitesElement.Collection.Count; i++) { 
      var siteElement = sitesElement.Collection[i]; 
      if (siteElement.Properties["name"].Value.Equals(siteName) && 
       siteElement.Properties["id"].Value.Equals(siteId)) { 
       newSiteElement = siteElement; 
       break; 
      } 
     } 

     if (newSiteElement == null) { 
      // 
      // Site doesn't exist yet. Add new site node 
      // 

      newSiteElement = sitesElement.Collection.CreateNewElement(""); 
      newSiteElement.Properties["id"].Value = siteId; 
      newSiteElement.Properties["name"].Value = siteName; 
      fNewSite = true; 
     } 

     // setup bindings for the new site 

     var ftpBindingString = "*:21:"; 

     var Bindings = newSiteElement.GetElementByName("bindings"); 
     var BindingElement = Bindings.Collection.CreateNewElement(""); 
     BindingElement.Properties["protocol"].Value = "ftp"; 
     BindingElement.Properties["bindingInformation"].Value = ftpBindingString; 

     try { 
      Bindings.Collection.AddElement(BindingElement, 0); 
     } 
     catch (Exception ex) { 
      if (ex.Message != "") // ERROR_ALREADY_EXISTS ? 
      { 
       throw; 
      } 
     } 

     IAppHostElement newApplication = null; 
     // 
     // check if root application already exists 
     // 
     for (var i = 0; i < newSiteElement.Collection.Count; i++) { 
      var applicationElement = newSiteElement.Collection[i]; 

      if (applicationElement.Properties["path"].Value.Equals("/")) { 
       newApplication = applicationElement; 
       break; 
      } 
     } 

     if (newApplication == null) { 
      newApplication = newSiteElement.Collection.CreateNewElement("application"); 
      newApplication.Properties["path"].Value = "/"; 
      fNewApplication = true; 
     } 

     IAppHostElement newVirtualDirectory = null; 
     // 
     // search for the root vdir 
     // 
     for (var i = 0; i < newApplication.Collection.Count; i++) { 
      var vdirElement = newApplication.Collection[i]; 
      if (vdirElement.Properties["path"].Value.Equals("/")) { 
       newVirtualDirectory = vdirElement; 
       break; 
      } 
     } 
     if (newVirtualDirectory == null) { 
      newVirtualDirectory = newApplication.Collection.CreateNewElement(""); 
      newVirtualDirectory.Properties["path"].Value = "/"; 
      fNewVDir = true; 
     } 
     newVirtualDirectory.Properties["physicalPath"].Value = siteRoot; 
     if (fNewVDir) { 
      newApplication.Collection.AddElement(newVirtualDirectory, 0); 
     } 
     if (fNewApplication) { 
      newSiteElement.Collection.AddElement(newApplication, 0); 
     } 


     var ftpSiteSettings = newSiteElement.GetElementByName("ftpServer").GetElementByName("security").GetElementByName("authentication"); 

     Console.WriteLine("Enable anonymous authentication"); 

     var anonAuthSettings = ftpSiteSettings.GetElementByName("anonymousAuthentication"); 
     anonAuthSettings.Properties["enabled"].Value = "true"; 

     Console.WriteLine("Disable basic authentication"); 
     var basicAuthSettings = ftpSiteSettings.GetElementByName("basicAuthentication"); 
     basicAuthSettings.Properties["enabled"].Value = "false"; 

     BindingElement.Properties["bindingInformation"].Value = "*:21:"; 
     // 
     // Time to add new site element and commit changes 
     // 
     if (fNewSite) { 
      sitesElement.Collection.AddElement(newSiteElement, 0); 
     } 
     adminManager.CommitChanges(); 
    } 
    catch (Exception ex) { 
     Console.WriteLine("Error occured in AddDefaultFtpSite: " + ex.Message); 
    } 

    // 
    // Add <authorization> section to allow everyone Read 
    // 
    Console.WriteLine("Enable everyone Read access"); 
    try { 
     configPath = "MACHINE/WEBROOT/APPHOST/" + siteName; 
     configSectionName = "system.ftpServer/security/authorization"; 

     var azSection = adminManager.GetAdminSection(configSectionName, configPath); 

     azSection.Collection.Clear(); 

     var newAzElement = azSection.Collection.CreateNewElement(""); 
     newAzElement.Properties["accessType"].Value = "Allow"; 
     newAzElement.Properties["users"].Value = "*"; 
     newAzElement.Properties["permissions"].Value = "Read"; 

     azSection.Collection.AddElement(newAzElement, 0); 
     adminManager.CommitChanges(); 
    } 
    catch (Exception ex) { 
     Console.WriteLine("Error occured while adding authorization section: " + ex.Message); 
    } 
} 
+0

von http://blogs.msdn.com/b/robert_mcmurray/archive/2007/07/05/viewing-current-ftp7-sessions-using-c.aspx - Referenz% WinDir% \ System32 \ InetSrv \ nativrd. DLL – JDPeckham

Verwandte Themen