文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-12-03 09:40

关注

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

2、站点列表

  1. ///  
  2.         /// iis6获取所有ftp站点信息 
  3.         ///  
  4.         /// name="newsitename"
  5.         /// <returns>returns
  6.         public static List iGetFtpInfos() 
  7.         { 
  8.             List ftpinfos = new List(); 
  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.                 var siteEntry = new DirectoryEntry("IIS://localhost/MSFTPSVC");//IIS6管理对象 
  27.                 DirectoryEntry rootentry = new DirectoryEntry("IIS://localhost/W3SVC");//创建IIS管理对象 
  28.                 foreach (DirectoryEntry sitechild in siteEntry.Children) 
  29.                 { 
  30.                     if (!sitechild.SchemaClassName.EqualsEx("IIsFtpServer"))    //IIsFtpServer代表FTP 
  31.                         continue
  32.                     string yftpname = sitechild.Properties["ServerComment"].Value.ToString(); 
  33.                     string defaultftpname = System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp"); 
  34.                     if (yftpname == defaultftpname) 
  35.                     { 
  36.                         try 
  37.                         { 
  38.                             //获取站点信息 
  39.                             var root = sitechild.Children.Find("ROOT""IIsFtpVirtualDir"); 
  40.                             DirectoryEntries ftps = root.Children; 
  41.                             foreach (DirectoryEntry ftp in ftps) 
  42.                             { 
  43.                                 ftpname = ftp.Name
  44.                                  
  45.                                 try 
  46.                                 { 
  47.                                      
  48.                                     foreach (DirectoryEntry child in rootentry.Children) 
  49.                                     { 
  50.                                         if (child.SchemaClassName == "IIsWebServer" && child.Properties["ServerComment"].Value.ToString() == ftpname) 
  51.                                         { 
  52.                                             ftppass = child.Properties["AnonymousUserPass"].Value.ToString(); 
  53.                                              
  54.                                             foreach (DirectoryEntry rootChild in child.Children) 
  55.                                             { 
  56.                                                 string name = rootChild.Name.ToString(); 
  57.                                                 if ((rootChild.SchemaClassName == "IIsWebVirtualDir") && (rootChild.Name.ToString().ToLower() == "root")) 
  58.                                                 { 
  59.                                                     if (rootChild.Properties["Path"].Value == null
  60.                                                     { 
  61.                                                         ftppath = ""
  62.                                                     } 
  63.                                                     else 
  64.                                                     { 
  65.                                                         ftppath = rootChild.Properties["Path"].Value.ToString().Substring(0, rootChild.Properties["Path"].Value.ToString().LastIndexOf("\\")); 
  66.                                                     } 
  67.                                                 } 
  68.                                             } 
  69.                                         } 
  70.                                     } 
  71.                                 } 
  72.                                 catch 
  73.                                 { 
  74.  
  75.                                 } 
  76.                                  
  77.                                 if(ftpname != ""
  78.                                     ftpinfos.Add(ftproot + "-@-" + ftpname + "-@-" + ftppass + "-@-" + ftppath + "-@-" + iisversion);//添加到站点信息 
  79.                             } 
  80.                         } 
  81.                         catch 
  82.                         { 
  83.  
  84.                         } 
  85.                     } 
  86.                 } 
  87.             } 
  88.             catch 
  89.             { 
  90.             } 
  91.             return ftpinfos;//返回数据 
  92.         } 

删除站点

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

 

来源:后端Q内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯