本文转载自微信公众号「后端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();
-
- try
- {
-
- 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
- }
-
- 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"));
-
- System.IO.File.SetAttributes(config_rootpath, System.IO.FileAttributes.Hidden);
- }
- catch
- {
-
- }
- }
- catch
- {
- errorCode = ErrorCode.ftpSiteFail;
- }
-
- }
- else
- {
- if (!getFtpState(ftpname))//判断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;
- }
2、站点列表
- ///
- /// iis6获取所有ftp站点信息
- ///
- /// name="newsitename">
- /// <returns>returns>
- public static List
iGetFtpInfos() - {
- List
ftpinfos = new List(); - 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();
- }
-
- 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;//返回数据
- }
删除站点
- public static bool DeleteQFtp(string ftpname)
- {
- bool flag = false;
- try{
-
-
- 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;
- }