欧美成人精品手机在线观看_69视频国产_动漫精品第一页_日韩中文字幕网 - 日本欧美一区二区

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

【C#】FTPHelper-封裝FTP的相關(guān)操作

admin
2021年5月11日 17:32 本文熱度 2961

using System;

using System.IO;

using System.Net;

using System.Text;

 

namespace Whir.Software.DataSyncTools.Library.Helper

{

    /// <summary>

    ///     Ftp輔助類

    /// </summary>

    public class FtpHelper

    {

        private const int BufferSize = 2048;

        private readonly string _host;

        private readonly string _pass;

        private readonly string _user;

        private FtpWebRequest _ftpRequest;

        private FtpWebResponse _ftpResponse;

        private Stream _ftpStream;

 

        public FtpHelper(string hostIp, string userName, string password)

        {

            _host = hostIp;

            _user = userName;

            _pass = password;

        }

 

        /// <summary>

        ///     下載文件

        /// </summary>

        /// <param name="localFile"></param>

        /// <param name="remoteFile"></param>

        /// <returns></returns>

        public FtpResult Download(string localFile, string remoteFile)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + remoteFile);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpStream = _ftpResponse.GetResponseStream();

                var localFileStream = new FileStream(localFile, FileMode.Create);

                var byteBuffer = new byte[BufferSize];

                if (_ftpStream != null)

                {

                    int bytesRead = _ftpStream.Read(byteBuffer, 0, BufferSize);

                    try

                    {

                        while (bytesRead > 0)

                        {

                            localFileStream.Write(byteBuffer, 0, bytesRead);

                            bytesRead = _ftpStream.Read(byteBuffer, 0, BufferSize);

                        }

                    }

                    catch (Exception ex)

                    {

                        result = new FtpResult(false, ex.Message);

                        return result;

                    }

                }

                localFileStream.Close();

                if (_ftpStream != null) _ftpStream.Close();

                _ftpResponse.Close();

                _ftpRequest = null;

                result = new FtpResult(true, "ok");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     上傳文件

        /// </summary>

        /// <param name="localFile"></param>

        /// <param name="remoteFile"></param>

        /// <returns></returns>

        public FtpResult Upload(string localFile, string remoteFile)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + remoteFile);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

                _ftpStream = _ftpRequest.GetRequestStream();

                var localFileStream = new FileStream(localFile, FileMode.Create);

                var byteBuffer = new byte[BufferSize];

                int bytesSent = localFileStream.Read(byteBuffer, 0, BufferSize);

                try

                {

                    while (bytesSent != 0)

                    {

                        _ftpStream.Write(byteBuffer, 0, bytesSent);

                        bytesSent = localFileStream.Read(byteBuffer, 0, BufferSize);

                    }

                }

                catch (Exception ex)

                {

                    result = new FtpResult(false, ex.Message);

                    return result;

                }

                localFileStream.Close();

                _ftpStream.Close();

                _ftpRequest = null;

                result = new FtpResult(true, "ok");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     刪除文件

        /// </summary>

        /// <param name="deleteFile"></param>

        public FtpResult Delete(string deleteFile)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + deleteFile);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpResponse.Close();

                _ftpRequest = null;

                result = new FtpResult(true, "ok");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     文件重命名

        /// </summary>

        /// <param name="currentFileNameAndPath"></param>

        /// <param name="newFileName"></param>

        /// <returns></returns>

        public FtpResult Rename(string currentFileNameAndPath, string newFileName)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + currentFileNameAndPath);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.Rename;

                _ftpRequest.RenameTo = newFileName;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpResponse.Close();

                _ftpRequest = null;

                result = new FtpResult(true, "ok");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     創(chuàng)建目錄

        /// </summary>

        /// <param name="newDirectory"></param>

        /// <returns></returns>

        public FtpResult CreateDirectory(string newDirectory)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + newDirectory);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpResponse.Close();

                _ftpRequest = null;

                result = new FtpResult(true, "ok");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     取得文件創(chuàng)建時(shí)間

        /// </summary>

        /// <param name="fileName"></param>

        /// <returns></returns>

        public FtpResult GetFileCreatedDateTime(string fileName)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + fileName);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpStream = _ftpResponse.GetResponseStream();

                if (_ftpStream != null)

                {

                    var ftpReader = new StreamReader(_ftpStream);

                    string fileInfo;

                    try

                    {

                        fileInfo = ftpReader.ReadToEnd();

                    }

                    catch (Exception ex)

                    {

                        result = new FtpResult(false, ex.Message);

                        ftpReader.Close();

                        if (_ftpStream != null) _ftpStream.Close();

                        _ftpResponse.Close();

                        _ftpRequest = null;

                        return result;

                    }

                    ftpReader.Close();

                    if (_ftpStream != null) _ftpStream.Close();

                    _ftpResponse.Close();

                    _ftpRequest = null;

                    return new FtpResult(true, fileInfo);

                }

                return new FtpResult(false, "響應(yīng)流為空");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     取得文件大小

        /// </summary>

        /// <param name="fileName"></param>

        /// <returns></returns>

        public FtpResult GetFileSize(string fileName)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + fileName);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpStream = _ftpResponse.GetResponseStream();

                if (_ftpStream != null)

                {

                    var ftpReader = new StreamReader(_ftpStream);

                    string fileInfo = null;

                    try

                    {

                        while (ftpReader.Peek() != -1)

                        {

                            fileInfo = ftpReader.ReadToEnd();

                        }

                    }

                    catch (Exception ex)

                    {

                        result = new FtpResult(false, ex.Message);

                        ftpReader.Close();

                        if (_ftpStream != null) _ftpStream.Close();

                        _ftpResponse.Close();

                        _ftpRequest = null;

                        return result;

                    }

                    ftpReader.Close();

                    _ftpStream.Close();

                    _ftpResponse.Close();

                    _ftpRequest = null;

                    return new FtpResult(true, fileInfo);

                }

                result = new FtpResult(false, "響應(yīng)流為空");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     顯示遠(yuǎn)程目錄結(jié)構(gòu)

        /// </summary>

        /// <param name="directory"></param>

        /// <returns></returns>

        public string[] DirectoryListSimple(string directory)

        {

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + directory);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpStream = _ftpResponse.GetResponseStream();

                if (_ftpStream != null)

                {

                    var ftpReader = new StreamReader(_ftpStream);

                    string directoryRaw = null;

                    try

                    {

                        while (ftpReader.Peek() != -1)

                        {

                            directoryRaw += ftpReader.ReadLine() + "|";

                        }

                    }

                    catch (Exception ex)

                    {

                        Console.WriteLine(ex.ToString());

                    }

                    ftpReader.Close();

                    _ftpStream.Close();

                    _ftpResponse.Close();

                    _ftpRequest = null;

                    /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */

                    try

                    {

                        if (directoryRaw != null)

                        {

                            string[] directoryList = directoryRaw.Split("|".ToCharArray());

                            return directoryList;

                        }

                    }

                    catch (Exception ex)

                    {

                        Console.WriteLine(ex.ToString());

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.ToString());

            }

            return new[] { "" };

        }

 

        /// <summary>

        ///     遠(yuǎn)程文件列表

        /// </summary>

        /// <param name="directory"></param>

        /// <returns></returns>

        public string[] DirectoryListDetailed(string directory)

        {

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + directory);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpStream = _ftpResponse.GetResponseStream();

                if (_ftpStream != null)

                {

                    var ftpReader = new StreamReader(_ftpStream);

                    string directoryRaw = null;

                    try

                    {

                        while (ftpReader.Peek() != -1)

                        {

                            directoryRaw += ftpReader.ReadLine() + "|";

                        }

                    }

                    catch (Exception ex)

                    {

                        Console.WriteLine(ex.ToString());

                    }

                    ftpReader.Close();

                    _ftpStream.Close();

                    _ftpResponse.Close();

                    _ftpRequest = null;

                    try

                    {

                        if (directoryRaw != null)

                        {

                            string[] directoryList = directoryRaw.Split("|".ToCharArray());

                            return directoryList;

                        }

                    }

                    catch (Exception ex)

                    {

                        Console.WriteLine(ex.ToString());

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.ToString());

            }

            /* Return an Empty string Array if an Exception Occurs */

            return new[] { "" };

        }

    }

 

    public class FtpResult

    {

        public FtpResult(bool isCusecess, string message)

        {

            IsSucess = isCusecess;

            Message = message;

        }

 

        public bool IsSucess { get; set; }

        public string Message { get; set; }

    }

}


該文章在 2021/5/11 17:33:25 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved