【问题标题】:How do I programmatically create a FTP site in IIS7 on Windows7?如何在 Windows 7 上以编程方式在 IIS 7 中创建 FTP 站点?
【发布时间】:2010-10-13 22:04:03
【问题描述】:

我希望使用批处理文件执行此步骤:“通过编辑 IIS 7.0 配置文件创建新的 FTP 站点”,并且想知道是否有人已经这样做了?

http://learn.iis.net/page.aspx/301/creating-a-new-ftp-site/

【问题讨论】:

    标签: iis iis-7 windows-7 ftp


    【解决方案1】:

    试试这个。需要引用COM组件“AppHostAdminLibrary”

    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);
        }
    }
    

    【讨论】:

    【解决方案2】:

    【讨论】:

    • 看起来可能不错。有没有类似c#写的sn-p?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多