欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# 在windows中操作IIS設(shè)置FTP服務(wù)器的示例

 更新時(shí)間:2021年03月15日 08:32:52   作者:conan  
這篇文章主要介紹了c# 在windows中操作IIS設(shè)置FTP服務(wù)器的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

什么是FTP

FTP(File Transfer Protocol)是TCP/IP網(wǎng)絡(luò)上兩臺(tái)計(jì)算機(jī)傳送文件的協(xié)議,使得主機(jī)間可以共享文件.可以將 Internet 信息服務(wù) (IIS) 配置為作為 FTP 服務(wù)器來運(yùn)行。 這樣,其他計(jì)算機(jī)便可以連接到服務(wù)器并將文件復(fù)制到服務(wù)器或者從服務(wù)器復(fù)制文件。 例如,如果您在自己的計(jì)算機(jī)上承載網(wǎng)站,并且希望允許遠(yuǎn)程用戶連接到您的計(jì)算機(jī)并將他們的文件復(fù)制到服務(wù)器,則可以將 IIS 配置為充當(dāng) FTP 服務(wù)器。

主要實(shí)現(xiàn)方式

下面主要講解一下,在Window的IIS中創(chuàng)建FTP的Site。

1、創(chuàng)建站點(diǎn)

 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)
            {
              /*
              * 站點(diǎn)描述
              */
              string sitename = ftpsite.Name;
              /*
              * 站點(diǎn)綁定域名和端口
              */
              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
          }
          /*
           * 創(chuàng)建FTP
          */
          if (!System.IO.Directory.Exists(System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath")))//創(chuàng)建站點(diǎn)路徑
          {
            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();
          //設(shè)置FTP SSL權(quán)限
          SetFtpSSL();
          //設(shè)置FTP Everyone權(quán)限
          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狀態(tài)檢查 ----*/
          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訪問權(quán)限
              SetFtpAccess(ftpname);
            }
          }
          catch
          {
            errorCode = ErrorCode.ftpSiteFail;
          }
        }
        else
        {
          errorCode = ErrorCode.ftpExists;
        }

      }
      return errorCode;
    }

2、站點(diǎn)列表

/// <summary>
    /// iis6獲取所有ftp站點(diǎn)信息
    /// </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();
        }
        /*
         * 創(chuàng)建FTP 子站點(diǎn)
         */
        var siteEntry = new DirectoryEntry("IIS://localhost/MSFTPSVC");//IIS6管理對(duì)象
        DirectoryEntry rootentry = new DirectoryEntry("IIS://localhost/W3SVC");//創(chuàng)建IIS管理對(duì)象
        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
            {
              //獲取站點(diǎn)信息
              var root = sitechild.Children.Find("ROOT", "IIsFtpVirtualDir");
              DirectoryEntries ftps = root.Children;
              foreach (DirectoryEntry ftp in ftps)
              {
                ftpname = ftp.Name;
                /*
                 * 獲取密碼
                 */
                try
                {
                  /*
                  * 循環(huán)站點(diǎn)獲取站點(diǎn)信息
                  */
                  foreach (DirectoryEntry child in rootentry.Children)
                  {
                    if (child.SchemaClassName == "IIsWebServer" && child.Properties["ServerComment"].Value.ToString() == ftpname)
                    {
                      ftppass = child.Properties["AnonymousUserPass"].Value.ToString();
                      /*
                       * 獲取站點(diǎn)目錄
                       */
                      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);//添加到站點(diǎn)信息
              }
            }
            catch
            {

            }
          }
        }
      }
      catch
      {
      }
      return ftpinfos;//返回?cái)?shù)據(jù)
    }

3、刪除站點(diǎn)

 public static bool DeleteQFtp(string ftpname)
    {
      bool flag = false;
      try{

        /*
        * 刪除FTP 子站點(diǎn)
        */
        var siteEntry = new DirectoryEntry("IIS://localhost/MSFTPSVC");//IIS6管理對(duì)象
        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設(shè)置FTP服務(wù)器的示例的詳細(xì)內(nèi)容,更多關(guān)于c# 設(shè)置FTP服務(wù)器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 一則C#簡潔瀑布流代碼

    一則C#簡潔瀑布流代碼

    最近想實(shí)現(xiàn)數(shù)據(jù)的延遲加載,網(wǎng)上找一下有很多例子,看了Masonry的例子啟發(fā),自己寫了一個(gè)很簡潔的代碼。分享給大家
    2014-06-06
  • 直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET

    直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET

    這篇文章主要用asp.net技術(shù)實(shí)現(xiàn)直接在線預(yù)覽word、excel、txt文件,有需要的朋友可以參考下
    2015-08-08
  • C#實(shí)現(xiàn)文本讀取的7種方式

    C#實(shí)現(xiàn)文本讀取的7種方式

    這篇文章主要介紹了C#實(shí)現(xiàn)文本讀取的7種方式,文本讀取在上位機(jī)開發(fā)中經(jīng)常會(huì)使用到,實(shí)現(xiàn)的方式也有很多種,下面我們就來分享七種方式,需要的小伙伴可以參考一下
    2022-05-05
  • C# DataTable中查詢指定字段名稱的數(shù)據(jù)

    C# DataTable中查詢指定字段名稱的數(shù)據(jù)

    這篇文章主要介紹了C# DataTable中查詢指定字段名稱的數(shù)據(jù),本文直接給出實(shí)例代碼,簡單易懂,需要的朋友可以參考下
    2015-06-06
  • C#.NET 圖片水印添加代碼

    C#.NET 圖片水印添加代碼

    這篇文章主要為大家詳細(xì)介紹了C#.NET 圖片水印添加代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • C#中的Task.WaitAll和Task.WaitAny方法介紹

    C#中的Task.WaitAll和Task.WaitAny方法介紹

    這篇文章介紹了C#中的Task.WaitAll和Task.WaitAny方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • c# 使用WebRequest實(shí)現(xiàn)多文件上傳

    c# 使用WebRequest實(shí)現(xiàn)多文件上傳

    這篇文章主要介紹了c# 使用WebRequest實(shí)現(xiàn)多文件上傳的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#中構(gòu)造函數(shù)和析構(gòu)函數(shù)用法實(shí)例詳解

    C#中構(gòu)造函數(shù)和析構(gòu)函數(shù)用法實(shí)例詳解

    這篇文章主要介紹了C#中構(gòu)造函數(shù)和析構(gòu)函數(shù)用法,結(jié)合實(shí)例形式詳細(xì)分析了C#中構(gòu)造函數(shù)與析構(gòu)函數(shù)的原理、定義、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-06-06
  • C#基于Socket的網(wǎng)絡(luò)通信類你了解嗎

    C#基于Socket的網(wǎng)絡(luò)通信類你了解嗎

    這篇文章主要為大家詳細(xì)介紹了C#基于Socket的網(wǎng)絡(luò)通信類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • C#?Razor語法規(guī)則

    C#?Razor語法規(guī)則

    這篇文章介紹了C#?Razor的語法規(guī)則,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01

最新評(píng)論