网钛远程桌面管理助手
当前位置:首页 > 服务器资讯

服务器资讯

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

时间:2021-03-13 17:56:07   作者:   来源:   阅读:161   评论:0
内容摘要: 即将开播:4月29日,民生银行郭庆谈商业银行金融科技赋能的探索与实践--> 本文转载自微信公众号「后端Q」,作者conan。转载本文请联系后端Q公众号。什么是FTPFTP(File Transfer Protocol)是TCP/IP网络上两台计算机传送文件的协议,使得主机......

即将开播:4月29日,民生银行郭庆谈商业银行金融科技赋能的探索与实践

-->

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

 什么是FTP

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

主要实现方式

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

1、创建站点

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

2、站点列表

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

3、删除站点

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


标签:quot  System  ConfigurationMan  站点  Configuration  

相关文章

相关评论