C#如何在Windows中操作IIS设置FTP服务器

服务器
FTP(File Transfer Protocol)是TCP/IP网络上两台计算机传送文件的协议,使得主机间可以共享文件.可以将 Internet 信息服务 (IIS) 配置为作为 FTP 服务器来运行。

[[387377]]

 本文转载自微信公众号「后端Q」,作者conan。转载本文请联系后端Q公众号。

 什么是FTP

FTP(File Transfer Protocol)是TCP/IP网络上两台计算机传送文件的协议,使得主机间可以共享文件.可以将 Internet 信息服务 (IIS) 配置为作为 FTP 服务器来运行。 这样,其他计算机便可以连接到服务器并将文件复制到服务器或者从服务器复制文件。 例如,如果您在自己的计算机上承载网站,并且希望允许远程用户连接到您的计算机并将他们的文件复制到服务器,则可以将 IIS 配置为充当 FTP 服务器。

主要实现方式

下面主要讲解一下,在Window的IIS中创建FTP的Site。

1、创建站点

public int createFtpSite(string ftpname,string path){ 
 
            int errorCode = ErrorCode.Succeed; 
            if (ftpname == "" && path == ""
            { 
                try 
                { 
                    ServerManager iisManager = new ServerManager(); 
                    Configuration cfg = iisManager.GetApplicationHostConfiguration(); 
                    /*---- 停止21端口 ----*/ 
                    try 
                    { 
                        /*---- sites ----*/ 
                        foreach (var ftpsite in iisManager.Sites) 
                        { 
                            /* 
                            * 站点描述 
                            */ 
                            string sitename = ftpsite.Name
                            /* 
                            * 站点绑定域名和端口 
                            */ 
                            foreach (Binding binding in ftpsite.Bindings) 
                            { 
                                try 
                                { 
                                    string currentServerBindings = binding.GetAttributeValue("BindingInformation").ToString(); 
                                    string port = currentServerBindings.Split(":".ToArray())[1]; 
                                    if (port == "21"
                                    { 
                                        try 
                                        { 
                                            //stop site 
                                            ftpsite.Stop(); 
                                        } 
                                        catch 
                                        { 
                                            //doing nothing 
                                        } 
                                        break; 
                                    } 
                                } 
                                catch 
                                { 
                                    //doing nothing 
                                } 
                            } 
                        } 
                        //提交更改 
                        iisManager.CommitChanges(); 
                    } 
                    catch 
                    { 
                        //do nothing 
                    } 
                    /* 
                     * 创建FTP 
                    */ 
                    if (!System.IO.Directory.Exists(System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath")))//创建站点路径 
                    { 
                        System.IO.Directory.CreateDirectory(System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath")); 
                    } 
                    Site site = iisManager.Sites.Add(System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp"), "ftp", string.Format("*:{0}:""21"), System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath")); 
                    iisManager.CommitChanges(); 
                    //设置FTP SSL权限 
                    SetFtpSSL(); 
                    //设置FTP Everyone权限 
                    IISUtil.IISCore.AddSiteUtil addsiteUtil = new AddSiteUtil(); 
                    try 
                    { 
                        string config_rootpath = System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath"); 
                        //string rootpath = path.Substring(0, path.IndexOf(ftpname) - 1) + "\\ftproot"
                        if (!System.IO.Directory.Exists(config_rootpath)) 
                        { 
                            System.IO.Directory.CreateDirectory(config_rootpath); 
                        } 
                        addsiteUtil.icaclsSet("Everyone", System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath")); 
                        /*---- hide ----*/ 
                        System.IO.File.SetAttributes(config_rootpath, System.IO.FileAttributes.Hidden); 
                    } 
                    catch 
                    { 
 
                    } 
                } 
                catch 
                { 
                    errorCode = ErrorCode.ftpSiteFail; 
                } 
                 
            } 
            else 
            { 
                if (!getFtpState(ftpname))//判断ftp用户是否存在 
                { 
                    /*---- FTP状态检查 ----*/ 
                    FtpStateInit(); 
                    try 
                    { 
                        using (ServerManager iisManager = new ServerManager()) 
                        { 
                            Site site = iisManager.Sites.FirstOrDefault(o => ((string)o["name"]).Contains(System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp"))); 
                            var vird = site.Applications[0].VirtualDirectories["/" + ftpname]; 
                            if (vird == null) { site.Applications[0].VirtualDirectories.Add("/" + ftpname, path); } 
                            else { errorCode = ErrorCode.ftpExists; } 
                            iisManager.CommitChanges(); 
                            //添加FTP访问权限 
                            SetFtpAccess(ftpname); 
                        } 
                    } 
                    catch 
                    { 
                        errorCode = ErrorCode.ftpSiteFail; 
                    } 
                } 
                else 
                { 
                    errorCode = ErrorCode.ftpExists; 
                } 
 
            } 
            return errorCode; 
        } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.

2、站点列表

/// <summary> 
        /// iis6获取所有ftp站点信息 
        /// </summary> 
        /// <param name="newsitename"></param> 
        /// <returns></returns
        public static List<string> iGetFtpInfos() 
        { 
            List<string> ftpinfos = new List<string>(); 
            try 
            { 
                string ftproot = System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp"); 
                string ftpname = "";//用户名 
                string ftppass = "";//密码 
                string ftppath = "";//物理路径 
                string iisversion = "";//iis版本 
                string majorversion = IISCore.IISInfoUtil.SGetIISMajorVersion(); 
                if (majorversion == ""
                { 
                    iisversion = "未知"
                } 
                else 
                { 
                    iisversion = majorversion.ToString(); 
                } 
                /* 
                 * 创建FTP 子站点 
                 */ 
                var siteEntry = new DirectoryEntry("IIS://localhost/MSFTPSVC");//IIS6管理对象 
                DirectoryEntry rootentry = new DirectoryEntry("IIS://localhost/W3SVC");//创建IIS管理对象 
                foreach (DirectoryEntry sitechild in siteEntry.Children) 
                { 
                    if (!sitechild.SchemaClassName.EqualsEx("IIsFtpServer"))    //IIsFtpServer代表FTP 
                        continue
                    string yftpname = sitechild.Properties["ServerComment"].Value.ToString(); 
                    string defaultftpname = System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp"); 
                    if (yftpname == defaultftpname) 
                    { 
                        try 
                        { 
                            //获取站点信息 
                            var root = sitechild.Children.Find("ROOT""IIsFtpVirtualDir"); 
                            DirectoryEntries ftps = root.Children; 
                            foreach (DirectoryEntry ftp in ftps) 
                            { 
                                ftpname = ftp.Name
                                /* 
                                 * 获取密码 
                                 */ 
                                try 
                                { 
                                    /* 
                                    * 循环站点获取站点信息 
                                    */ 
                                    foreach (DirectoryEntry child in rootentry.Children) 
                                    { 
                                        if (child.SchemaClassName == "IIsWebServer" && child.Properties["ServerComment"].Value.ToString() == ftpname) 
                                        { 
                                            ftppass = child.Properties["AnonymousUserPass"].Value.ToString(); 
                                            /* 
                                             * 获取站点目录 
                                             */ 
                                            foreach (DirectoryEntry rootChild in child.Children) 
                                            { 
                                                string name = rootChild.Name.ToString(); 
                                                if ((rootChild.SchemaClassName == "IIsWebVirtualDir") && (rootChild.Name.ToString().ToLower() == "root")) 
                                                { 
                                                    if (rootChild.Properties["Path"].Value == null
                                                    { 
                                                        ftppath = ""
                                                    } 
                                                    else 
                                                    { 
                                                        ftppath = rootChild.Properties["Path"].Value.ToString().Substring(0, rootChild.Properties["Path"].Value.ToString().LastIndexOf("\\")); 
                                                    } 
                                                } 
                                            } 
                                        } 
                                    } 
                                } 
                                catch 
                                { 
 
                                } 
                                /* 
                                 * 获取路径 
                                 */ 
                                if(ftpname != ""
                                    ftpinfos.Add(ftproot + "-@-" + ftpname + "-@-" + ftppass + "-@-" + ftppath + "-@-" + iisversion);//添加到站点信息 
                            } 
                        } 
                        catch 
                        { 
 
                        } 
                    } 
                } 
            } 
            catch 
            { 
            } 
            return ftpinfos;//返回数据 
        } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.

3、删除站点

public static bool DeleteQFtp(string ftpname) 
        { 
            bool flag = false
            try{ 
 
                /* 
                * 删除FTP 子站点 
                */ 
                var siteEntry = new DirectoryEntry("IIS://localhost/MSFTPSVC");//IIS6管理对象 
                if (ftpname != ""
                { 
                    foreach (DirectoryEntry sitechild in siteEntry.Children) 
                    { 
                        if (!sitechild.SchemaClassName.EqualsEx("IIsFtpServer"))    //IIsFtpServer代表FTP 
                            continue
                        string yftpname = sitechild.Properties["ServerComment"].Value.ToString(); 
                        if (yftpname.ToLower() == System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp").ToLower()) 
                        { 
                            try 
                            { 
                                DirectoryEntry root = sitechild.Children.Find("ROOT""IIsFtpVirtualDir"); 
                                var ftpchild = root.Children.Find(ftpname, "IIsFtpVirtualDir"); 
                                if (ftpchild != null
                                { 
                                    //删除 
                                    root.Children.Remove(ftpchild); 
                                    root.CommitChanges(); 
                                    sitechild.CommitChanges(); 
                                    siteEntry.CommitChanges(); 
                                    flag = true
                                } 
                            } 
                            catch 
                            { 
                                flag = false
                            } 
                        } 
                    } 
                } 
            } 
            catch 
            { 
            } 
            return flag; 
        } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

 

责任编辑:武晓燕 来源: 后端Q
相关推荐

2010-05-18 14:25:11

IIS服务器

2010-05-18 14:34:41

IIS服务器

2010-05-18 18:38:06

IIS服务器

2010-05-17 11:08:46

IIS服务器

2010-05-20 17:52:02

2010-06-30 15:40:11

IISWEB FTP服务器

2010-05-12 15:20:19

IIS 服务器

2017-03-17 14:05:48

LinuxUbuntuFTP服务器

2009-02-27 13:33:00

2022-07-18 10:15:14

文件传输协议TFTPLinux

2011-09-09 10:03:39

Ubuntu 11.0FTP服务器

2009-08-25 15:38:12

C# Windows服

2011-01-20 10:03:10

FTP服务器高级配置

2010-05-21 17:47:33

IIS服务器

2010-05-18 14:47:58

IIS服务器

2010-05-19 14:12:49

IIS FTP

2010-05-13 18:18:50

IIS服务器

2015-05-25 09:13:31

NTP网络时间协议NTP服务器

2022-08-24 08:33:27

Git系统Linux

2010-05-18 16:32:53

IIS服务器
点赞
收藏

51CTO技术栈公众号