c# 在windows中操作IIS設置FTP服務器的示例
什麼是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; }
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;//返回數據 }
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; }
以上就是c# 在windows中操作IIS設置FTP服務器的示例的詳細內容,更多關於c# 設置FTP服務器的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- C# .NET創建虛擬目錄的方法詳解
- C# 添加對System.Configuration.dll文件的引用操作
- c# 實現發送郵件到指定郵箱
- 為什麼ASP.NET Core 數據庫連接串的值和appsettings.json配的不一樣?
- .NET5控制臺程序使用EF連接MYSQL數據庫的方法