• 开源.NET FTP组件edtFTPnet 用法




    目前最新版本为2.2.3,下载后在bin目录中找到edtFTPnet.dll,在项目中添加引用。
    下面为一些用法:

    1、FTP服务器根目录为web,在它下面创建如下图示的目录和上传文件

    FTPConnection.CommandEncoding = Encoding.GetEncoding("GBK") 指定GBK编码以支持中文文件名
    FTPConnection.CreateDirectory() 创建指定目录
    FTPConnection.ChangeWorkingDirectory() 切换到指定目录
    FTPConnection.UploadFile() 上传文件

    web
    |-----L11
           |-----L21            
                  |-----L31
                          |-----31.htm
                  |-----21.htm
           |-----L22
           |-----11.htm
           |-----目录.txt

    [csharp] view plain copy
     
    print?
    1. private const string ftpServerIP = "xx.xx.xx.xx";  
    2. private const string ftpRemotePath = "web";  
    3. private const string ftpUserID = "user";  
    4. private const string ftpPassword = "123";  
    5.   
    6. private void btnCreateAndUpload_Click(object sender, EventArgs e)  
    7. {            
    8.     using (var conn = new FTPConnection  
    9.     {  
    10.         ServerAddress = ftpServerIP,  
    11.         ServerDirectory = ftpRemotePath,  
    12.         UserName = ftpUserID,  
    13.         Password = ftpPassword,  
    14.         CommandEncoding = Encoding.GetEncoding("GBK")  
    15.     })  
    16.     {  
    17.         conn.Connect();                  
    18.   
    19.         //创建目录L11  
    20.         conn.CreateDirectory("L11");  
    21.   
    22.         //============切换当前工作目录到L11============  
    23.         conn.ChangeWorkingDirectory("L11");  
    24.         //上行代码也可以用conn.ServerDirectory = "L11";  
    25.   
    26.         //上传"文档.txt"  
    27.         conn.UploadFile("文档.txt", "文档.txt");  
    28.   
    29.         //创建子目录L21,L22  
    30.         conn.CreateDirectory("L21");  
    31.         conn.CreateDirectory("L22");  
    32.   
    33.         //在当前工作目录L11上传文件"L11.htm"  
    34.         conn.UploadFile("11.htm", "11.htm");  
    35.   
    36.         //============切换当前工作目录到L21============  
    37.         conn.ChangeWorkingDirectory("L21");  
    38.   
    39.         //上传文件21.htm  
    40.         conn.UploadFile("21.htm", "21.htm");  
    41.         //创建目录L31  
    42.         conn.CreateDirectory("L31");  
    43.   
    44.         //============切换当前工作目录到L31============  
    45.         conn.ChangeWorkingDirectory("L31");  
    46.   
    47.         //上传文件31.htm  
    48.         conn.UploadFile("31.htm", "31.htm");  
    49.     }  
    50. }  
    private const string ftpServerIP = "xx.xx.xx.xx";
    private const string ftpRemotePath = "web";
    private const string ftpUserID = "user";
    private const string ftpPassword = "123";
    
    private void btnCreateAndUpload_Click(object sender, EventArgs e)
    {          
        using (var conn = new FTPConnection
        {
            ServerAddress = ftpServerIP,
            ServerDirectory = ftpRemotePath,
            UserName = ftpUserID,
            Password = ftpPassword,
            CommandEncoding = Encoding.GetEncoding("GBK")
        })
        {
            conn.Connect();                
    
            //创建目录L11
            conn.CreateDirectory("L11");
    
            //============切换当前工作目录到L11============
            conn.ChangeWorkingDirectory("L11");
            //上行代码也可以用conn.ServerDirectory = "L11";
    
            //上传"文档.txt"
            conn.UploadFile("文档.txt", "文档.txt");
    
            //创建子目录L21,L22
            conn.CreateDirectory("L21");
            conn.CreateDirectory("L22");
    
            //在当前工作目录L11上传文件"L11.htm"
            conn.UploadFile("11.htm", "11.htm");
    
            //============切换当前工作目录到L21============
            conn.ChangeWorkingDirectory("L21");
    
            //上传文件21.htm
            conn.UploadFile("21.htm", "21.htm");
            //创建目录L31
            conn.CreateDirectory("L31");
    
            //============切换当前工作目录到L31============
            conn.ChangeWorkingDirectory("L31");
    
            //上传文件31.htm
            conn.UploadFile("31.htm", "31.htm");
        }
    }
    2、获取文件信息

    FTPConnection.GetFileInfos() 获取文件和目录详细信息,结果的一个属性Dir为true表示为目录,否则为文件
    [csharp] view plain copy
     
    print?
    1. private void btnList_Click(object sender, EventArgs e)  
    2. {  
    3.     using (var conn = new FTPConnection  
    4.     {  
    5.         ServerAddress = ftpServerIP,  
    6.         ServerDirectory = ftpRemotePath,  
    7.         UserName = ftpUserID,  
    8.         Password = ftpPassword,  
    9.         CommandEncoding = Encoding.GetEncoding("GBK")  
    10.     })  
    11.     {  
    12.         conn.Connect();  
    13.         //获取指定目录的所有子目录和文件信息  
    14.         conn.ChangeWorkingDirectory("L11/L21");                 
    15.         var files1 = conn.GetFileInfos();  
    16.         ShowFileInfos(files1);  
    17.         /*结果如下: 
    18.          File, /web/L11/L21/21.htm, 21.htm, 11.2KB, 2013-9-26 14:51:00 
    19.          Dir, /web/L11/L21/L31, L31, 0KB, 2013-9-26 14:51:00 
    20.          */  
    21.   
    22.         ////返回上级目录,例子中为web/L11  
    23.         conn.ChangeWorkingDirectoryUp();  
    24.         var files2 = conn.GetFileInfosRecursive();  
    25.         ShowFileInfos(files2);  
    26.         /*结果如下: 
    27.         File, /web/L11/11.htm, 11.htm, 59.4KB, 2013-9-26 14:51:00 
    28.         Dir, /web/L11/L21, L21, 0KB, 2013-9-26 14:51:00 
    29.         Dir, /web/L11/L22, L22, 0KB, 2013-9-26 14:51:00 
    30.         File, /web/L11/文档.txt, 文档.txt, 40.8KB, 2013-9-26 14:51:00 
    31.         File, /web/L11/L21/21.htm, 21.htm, 11.2KB, 2013-9-26 14:51:00 
    32.         Dir, /web/L11/L21/L31, L31, 0KB, 2013-9-26 14:51:00 
    33.         File, /web/L11/L21/L31/31.htm, 31.htm, 5.6KB, 2013-9-26 14:51:00 
    34.          */  
    35.     }  
    36. }        
    37. void ShowFileInfos(FTPFile[] files)  
    38. {  
    39.     foreach (var file in files)  
    40.     {  
    41.         string info = string.Format("{0}, {1}, {2}, {3}KB, {4}",  
    42.             file.Dir ? "Dir" : "File", file.Path, file.Name, Math.Round(file.Size / 1024.0, 1), file.LastModified);  
    43.         richTextBox1.AppendText(info + " ");  
    44.     }  
    45.     richTextBox1.AppendText(" ");  
    46. }  
    private void btnList_Click(object sender, EventArgs e)
    {
        using (var conn = new FTPConnection
        {
            ServerAddress = ftpServerIP,
            ServerDirectory = ftpRemotePath,
            UserName = ftpUserID,
            Password = ftpPassword,
            CommandEncoding = Encoding.GetEncoding("GBK")
        })
        {
            conn.Connect();
            //获取指定目录的所有子目录和文件信息
            conn.ChangeWorkingDirectory("L11/L21");               
            var files1 = conn.GetFileInfos();
            ShowFileInfos(files1);
            /*结果如下:
             File, /web/L11/L21/21.htm, 21.htm, 11.2KB, 2013-9-26 14:51:00
             Dir, /web/L11/L21/L31, L31, 0KB, 2013-9-26 14:51:00
             */
    
            ////返回上级目录,例子中为web/L11
            conn.ChangeWorkingDirectoryUp();
            var files2 = conn.GetFileInfosRecursive();
            ShowFileInfos(files2);
            /*结果如下:
            File, /web/L11/11.htm, 11.htm, 59.4KB, 2013-9-26 14:51:00
            Dir, /web/L11/L21, L21, 0KB, 2013-9-26 14:51:00
            Dir, /web/L11/L22, L22, 0KB, 2013-9-26 14:51:00
            File, /web/L11/文档.txt, 文档.txt, 40.8KB, 2013-9-26 14:51:00
            File, /web/L11/L21/21.htm, 21.htm, 11.2KB, 2013-9-26 14:51:00
            Dir, /web/L11/L21/L31, L31, 0KB, 2013-9-26 14:51:00
            File, /web/L11/L21/L31/31.htm, 31.htm, 5.6KB, 2013-9-26 14:51:00
             */
        }
    }      
    void ShowFileInfos(FTPFile[] files)
    {
        foreach (var file in files)
        {
            string info = string.Format("{0}, {1}, {2}, {3}KB, {4}",
                file.Dir ? "Dir" : "File", file.Path, file.Name, Math.Round(file.Size / 1024.0, 1), file.LastModified);
            richTextBox1.AppendText(info + "
    ");
        }
        richTextBox1.AppendText("
    ");
    }
    其中递归获取指定目录的所有子目录和文件信息的扩展方法GetFileInfosRecursive如下:
    [csharp] view plain copy
     
    print?
    1. public static class FtpClientExtensions  
    2. {  
    3.     /// <summary>  
    4.     /// 递归获取目录的所有子目录和文件信息  
    5.     /// </summary>  
    6.     public static FTPFile[] GetFileInfosRecursive(this FTPConnection conn)  
    7.     {  
    8.         var resultList = new List<FTPFile>();  
    9.         var fileInfos = conn.GetFileInfos();  
    10.         resultList.AddRange(fileInfos);  
    11.         foreach (var fileInfo in fileInfos)  
    12.         {  
    13.             if (fileInfo.Dir)  
    14.             {  
    15.                 conn.ServerDirectory = fileInfo.Path;  
    16.                 resultList.AddRange(conn.GetFileInfosRecursive());  
    17.             }  
    18.         }  
    19.         return resultList.ToArray();  
    20.     }  
    21.     /// <summary>  
    22.     /// 递归删除目录(包括所有子目录和文件信息)  
    23.     /// </summary>  
    24.     public static void DeleteDirectoryRecursive(this FTPConnection conn, string directoryName)  
    25.     {  
    26.         conn.ChangeWorkingDirectory(directoryName);  
    27.         var fileInfos = conn.GetFileInfos();  
    28.         foreach (var fileInfo in fileInfos)  
    29.         {  
    30.             if (fileInfo.Dir)  
    31.                 conn.DeleteDirectoryRecursive(fileInfo.Name);  
    32.             else  
    33.                 conn.DeleteFile(fileInfo.Name);  
    34.         }  
    35.         conn.ChangeWorkingDirectoryUp();  
    36.         conn.DeleteDirectory(directoryName);  
    37.     }  
    38. }  
    public static class FtpClientExtensions
    {
        /// <summary>
        /// 递归获取目录的所有子目录和文件信息
        /// </summary>
        public static FTPFile[] GetFileInfosRecursive(this FTPConnection conn)
        {
            var resultList = new List<FTPFile>();
            var fileInfos = conn.GetFileInfos();
            resultList.AddRange(fileInfos);
            foreach (var fileInfo in fileInfos)
            {
                if (fileInfo.Dir)
                {
                    conn.ServerDirectory = fileInfo.Path;
                    resultList.AddRange(conn.GetFileInfosRecursive());
                }
            }
            return resultList.ToArray();
        }
        /// <summary>
        /// 递归删除目录(包括所有子目录和文件信息)
        /// </summary>
        public static void DeleteDirectoryRecursive(this FTPConnection conn, string directoryName)
        {
            conn.ChangeWorkingDirectory(directoryName);
            var fileInfos = conn.GetFileInfos();
            foreach (var fileInfo in fileInfos)
            {
                if (fileInfo.Dir)
                    conn.DeleteDirectoryRecursive(fileInfo.Name);
                else
                    conn.DeleteFile(fileInfo.Name);
            }
            conn.ChangeWorkingDirectoryUp();
            conn.DeleteDirectory(directoryName);
        }
    }
    3、删除目录及所有子目录和文件

    FTPConnection.DeleteDirectory()当删除的目录不为空时,会出错,需递归删除,
    递归删除目录扩展方法DeleteDirectoryRecursive见上面类FtpClientExtensions
    [csharp] view plain copy
     
    print?
    1. private void btnDelete_Click(object sender, EventArgs e)  
    2. {  
    3.     using (var conn = new FTPConnection  
    4.     {  
    5.         ServerAddress = ftpServerIP,  
    6.         ServerDirectory = ftpRemotePath,  
    7.         UserName = ftpUserID,  
    8.         Password = ftpPassword,  
    9.         CommandEncoding = Encoding.GetEncoding("GBK")  
    10.     })  
    11.     {  
    12.         conn.Connect();                
    13.         //递归删除目录及所有子目录和文件  
    14.         conn.DeleteDirectoryRecursive("L11");  
    15.         //如果删除单个文件,路径为当前目录的相对路径  
    16.         //conn.DeleteFile("L11/11.htm");  
    17.     }  
    18. }  
    private void btnDelete_Click(object sender, EventArgs e)
    {
        using (var conn = new FTPConnection
        {
            ServerAddress = ftpServerIP,
            ServerDirectory = ftpRemotePath,
            UserName = ftpUserID,
            Password = ftpPassword,
            CommandEncoding = Encoding.GetEncoding("GBK")
        })
        {
            conn.Connect();              
            //递归删除目录及所有子目录和文件
            conn.DeleteDirectoryRecursive("L11");
            //如果删除单个文件,路径为当前目录的相对路径
            //conn.DeleteFile("L11/11.htm");
        }
    }



     

       

  • 相关阅读:
    php+Nginx 安装手册
    PostgreSQL 生成uuid
    登陆服务器错误: Disconnected:No supported authentication methods available
    安装Tengine
    netty解决方法 io.netty.util.IllegalReferenceCountException: refCnt: 0, increment: 1
    IntelliJ Idea 2016,2017,2018 注册码 免费激活方法
    jdbc连接"Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driv"
    mysql错误:you are using update mode and you tried to update a table without a where that uses a key column to disable safe mode
    spring介绍;安装;使用
    设置maven仓库阿里镜像
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/5600603.html
Copyright © 2020-2023  润新知