服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - c# 在windows中操作IIS设置FTP服务器的示例

c# 在windows中操作IIS设置FTP服务器的示例

2022-11-08 11:56conan C#

这篇文章主要介绍了c# 在windows中操作IIS设置FTP服务器的示例,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下

什么是FTP

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

主要实现方式

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

1、创建站点

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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、站点列表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/// <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、删除站点

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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# 在windows中操作IIS设置FTP服务器的示例的详细内容,更多关于c# 设置FTP服务器的资料请关注服务器之家其它相关文章!

原文链接:https://mp.weixin.qq.com/s/LAp0DT4SLGMiVrj2OLK0Iw

延伸 · 阅读

精彩推荐
  • C#C#中反射和扩展方法如何运用

    C#中反射和扩展方法如何运用

    这篇文章主要为大家详细介绍了C#中反射和扩展方法的运用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    码上猿梦12172022-01-04
  • C#C#中截取字符串的的基本方法详解

    C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    翠花菇凉5802022-09-26
  • C#C#利用GDI+画图的基础实例教程

    C#利用GDI+画图的基础实例教程

    编写图形程序时需要使用GDI(Graphics Device Interface,图形设备接口),所以通过网上的相关资料整理了这篇文章,下面这篇文章主要给大家介绍了关于C#利用...

    Dandelion_drq4402022-02-22
  • C#C#简单实现防止多个程序运行的方法

    C#简单实现防止多个程序运行的方法

    这篇文章主要介绍了C#简单实现防止多个程序运行的方法,涉及C#进程操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Microblue4592021-11-14
  • C#c# 连接池的设置与使用

    c# 连接池的设置与使用

    这篇文章主要介绍了c# 连接池的设置与使用,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下...

    旷野风筝少年9632022-10-31
  • C#C#中的Lazy如何使用详解

    C#中的Lazy如何使用详解

    这篇文章主要给大家介绍了C#中Lazy如何使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    码农读书10822022-10-24
  • C#C#导出数据到excel如何提升性能

    C#导出数据到excel如何提升性能

    这篇文章主要介绍了C#导出数据到excel如何提升性能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参...

    叶丶梓轩11152022-09-24
  • C#C#引用类型和值类型的适用场合和区别

    C#引用类型和值类型的适用场合和区别

    今天小编就为大家分享一篇关于C#引用类型和值类型的适用场合和区别,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起...

    Mogul17922022-03-08