最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#調(diào)用第三方工具完成FTP操作

 更新時(shí)間:2022年05月16日 15:11:45   作者:springsnow  
這篇文章介紹了C#調(diào)用第三方工具完成FTP操作的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、FileZilla

Filezilla分為client和server。其中FileZilla Server是Windows平臺(tái)下一個(gè)小巧的第三方FTP服務(wù)器軟件,系統(tǒng)資源也占用非常小,可以讓你快速簡(jiǎn)單的建立自己的FTP服務(wù)器。

打開FileZilla,進(jìn)行如下操作

下圖紅色區(qū)域就是linux系統(tǒng)的文件目錄,可以直接把windows下的文件直接拖拽進(jìn)去。

二、WinSCP

跟FileZilla一樣,也是一款十分方便的文件傳輸工具。WinSCP是連接Windows和Linux的。

WinSCP .NET Assembly and SFTP

https://winscp.net/eng/docs/library#csharp

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
    SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Upload files
    TransferOptions transferOptions = new TransferOptions();
    transferOptions.TransferMode = TransferMode.Binary;

    TransferOperationResult transferResult;
    transferResult =  session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);

    // Throw on any error
    transferResult.Check();

    // Print results
    foreach (TransferEventArgs transfer in transferResult.Transfers)
    {
        Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
    }
}

三、FluentFTP

FluentFTP是一款老外開發(fā)的基于.Net的支持FTP及的FTPS 的FTP類庫(kù),F(xiàn)luentFTP是完全托管的FTP客戶端,被設(shè)計(jì)為易于使用和易于擴(kuò)展。它支持文件和目錄列表,上傳和下載文件和SSL / TLS連接。

它底層由Socket實(shí)現(xiàn),可以連接到Unix和Windows IIS建立FTP的服務(wù)器,

github:https://github.com/robinrodricks/FluentFTP

舉例:

// create an FTP client
FtpClient client = new FtpClient("123.123.123.123");

// if you don't specify login credentials, we use the "anonymous" user account
client.Credentials = new NetworkCredential("david", "pass123");

// begin connecting to the server
client.Connect();

// get a list of files and directories in the "/htdocs" folder
foreach (FtpListItem item in client.GetListing("/htdocs")) {
    
    // if this is a file
    if (item.Type == FtpFileSystemObjectType.File){
        
        // get the file size
        long size = client.GetFileSize(item.FullName);
        
    }
    
    // get modified date/time of the file or folder
    DateTime time = client.GetModifiedTime(item.FullName);
    
    // calculate a hash for the file on the server side (default algorithm)
    FtpHash hash = client.GetHash(item.FullName);
    
}

// upload a file
client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/big.txt");

// rename the uploaded file
client.Rename("/htdocs/big.txt", "/htdocs/big2.txt");

// download the file again
client.DownloadFile(@"C:\MyVideo_2.mp4", "/htdocs/big2.txt");

// delete the file
client.DeleteFile("/htdocs/big2.txt");

// delete a folder recursively
client.DeleteDirectory("/htdocs/extras/");

// check if a file exists
if (client.FileExists("/htdocs/big2.txt")){ }

// check if a folder exists
if (client.DirectoryExists("/htdocs/extras/")){ }

// upload a file and retry 3 times before giving up
client.RetryAttempts = 3;
client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/big.txt", FtpExists.Overwrite, false, FtpVerify.Retry);

// disconnect! good bye!
client.Disconnect();

對(duì)FluentFTP部分操作封裝類

public class FtpFileMetadata
{
    public long FileLength { get; set; }
    public string MD5Hash { get; set; }
    public DateTime LastModifyTime { get; set; }
}

public class FtpHelper
{
    private FtpClient _client = null;
    private string _host = "127.0.0.1";
    private int _port = 21;
    private string _username = "Anonymous";
    private string _password = "";
    private string _workingDirectory = "";
    public string WorkingDirectory
    {
        get
        {
            return _workingDirectory;
        }
    }
    public FtpHelper(string host, int port, string username, string password)
    {
        _host = host;
        _port = port;
        _username = username;
        _password = password;
    }

    public Stream GetStream(string remotePath)
    {
        Open();
        return _client.OpenRead(remotePath);
    }

    public void Get(string localPath, string remotePath)
    {
        Open();
        _client.DownloadFile(localPath, remotePath, true);
    }

    public void Upload(Stream s, string remotePath)
    {
        Open();
        _client.Upload(s, remotePath, FtpExists.Overwrite, true);
    }

    public void Upload(string localFile, string remotePath)
    {
        Open();
        using (FileStream fileStream = new FileStream(localFile, FileMode.Open))
        {
            _client.Upload(fileStream, remotePath, FtpExists.Overwrite, true);
        }
    }

    public int UploadFiles(IEnumerable<string> localFiles, string remoteDir)
    {
        Open();
        List<FileInfo> files = new List<FileInfo>();
        foreach (var lf in localFiles)
        {
            files.Add(new FileInfo(lf));
        }
        int count = _client.UploadFiles(files, remoteDir, FtpExists.Overwrite, true, FtpVerify.Retry);
        return count;
    }

    public void MkDir(string dirName)
    {
        Open();
        _client.CreateDirectory(dirName);
    }

    public bool FileExists(string remotePath)
    {
        Open();
        return _client.FileExists(remotePath);
    }
    public bool DirExists(string remoteDir)
    {
        Open();
        return _client.DirectoryExists(remoteDir);
    }

    public FtpListItem[] List(string remoteDir)
    {
        Open();
        var f = _client.GetListing();
        FtpListItem[] listItems = _client.GetListing(remoteDir);
        return listItems;
    }

    public FtpFileMetadata Metadata(string remotePath)
    {
        Open();
        long size = _client.GetFileSize(remotePath);
        DateTime lastModifyTime = _client.GetModifiedTime(remotePath);

        return new FtpFileMetadata()
        {
            FileLength = size,
            LastModifyTime = lastModifyTime
        };
    }

    public bool TestConnection()
    {
        return _client.IsConnected;
    }

    public void SetWorkingDirectory(string remoteBaseDir)
    {
        Open();
        if (!DirExists(remoteBaseDir))
            MkDir(remoteBaseDir);
        _client.SetWorkingDirectory(remoteBaseDir);
        _workingDirectory = remoteBaseDir;
    }
    private void Open()
    {
        if (_client == null)
        {
            _client = new FtpClient(_host, new System.Net.NetworkCredential(_username, _password));
            _client.Port = 21;
            _client.RetryAttempts = 3;
            if (!string.IsNullOrWhiteSpace(_workingDirectory))
            {
                _client.SetWorkingDirectory(_workingDirectory);
            }
        }
    }
}

到此這篇關(guān)于C#調(diào)用第三方工具完成FTP操作的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#畫筆Pen畫虛線的方法

    C#畫筆Pen畫虛線的方法

    這篇文章主要介紹了C#畫筆Pen畫虛線的方法,涉及C#畫筆Pen屬性的相關(guān)設(shè)置技巧,需要的朋友可以參考下
    2015-06-06
  • C#字符串自增自減算法詳解

    C#字符串自增自減算法詳解

    這篇文章主要為大家詳細(xì)介紹了C#字符串自增自減的算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C#實(shí)現(xiàn)類似jQuery的方法連綴功能

    C#實(shí)現(xiàn)類似jQuery的方法連綴功能

    這篇文章主要介紹了C#實(shí)現(xiàn)類似jQuery的方法連綴功能,可以簡(jiǎn)化語(yǔ)句,使代碼變得清晰簡(jiǎn)單,感興趣的小伙伴們可以參考一下
    2015-11-11
  • C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能

    C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能

    這篇文章主要介紹了C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能,需要的朋友可以參考下
    2017-12-12
  • C#連接SQL?Sever數(shù)據(jù)庫(kù)詳細(xì)圖文教程

    C#連接SQL?Sever數(shù)據(jù)庫(kù)詳細(xì)圖文教程

    C#是Microsoft公司為.NET Framework推出的重量級(jí)語(yǔ)言,和它搭配最完美的數(shù)據(jù)庫(kù)無(wú)疑就是Microsoft SQL Server了,下面這篇文章主要給大家介紹了關(guān)于C#連接SQL?Sever數(shù)據(jù)庫(kù)的詳細(xì)圖文教程,需要的朋友可以參考下
    2023-06-06
  • 深入多線程之:解析線程的交會(huì)(Thread Rendezvous)詳解

    深入多線程之:解析線程的交會(huì)(Thread Rendezvous)詳解

    本篇文章是對(duì)線程的交會(huì)(Thread Rendezvous)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#中TextBox實(shí)現(xiàn)輸入提示功能的方法

    C#中TextBox實(shí)現(xiàn)輸入提示功能的方法

    這篇文章主要介紹了C#中TextBox實(shí)現(xiàn)輸入提示功能的方法,涉及C#中TextBox的相關(guān)操作技巧,需要的朋友可以參考下
    2015-06-06
  • WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解

    WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解

    這篇文章主要介紹了WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • C#使用Spire.PDF for .NET刪除PDF文件中的圖層

    C#使用Spire.PDF for .NET刪除PDF文件中的圖層

    PDF 文件已成為我們?nèi)粘9ぷ骱蛯W(xué)習(xí)中不可或缺的一部分,然而,你是否曾遇到過(guò)這樣的困擾:一個(gè)看似簡(jiǎn)單的 PDF 文件,卻異常臃腫,打開緩慢,甚至在打印時(shí)出現(xiàn)多余內(nèi)容?這很可能與 PDF 圖層有關(guān),本文將深入探討如何刪除 PDF 文件中的圖層,需要的朋友可以參考下
    2025-09-09
  • C#向Word文檔中添加內(nèi)容控件的方法示例

    C#向Word文檔中添加內(nèi)容控件的方法示例

    這篇文章主要給大家介紹了C#向Word文檔中添加內(nèi)容控件的方法,文中對(duì)各種不同控件的添加方法分別進(jìn)行了介紹,如組合框、文本、圖片、日期選取器及下拉列表等內(nèi)容控件,都給出了詳細(xì)的示例代碼,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-01-01

最新評(píng)論

临安市| 浠水县| 永善县| 蒲江县| 比如县| 囊谦县| 视频| 柳林县| 长乐市| 稷山县| 邯郸市| 长阳| 库尔勒市| 屏南县| 永年县| 阜康市| 渝北区| 册亨县| 铁岭市| 和静县| 周口市| 苏尼特左旗| 炎陵县| 前郭尔| 浦城县| 吴堡县| 内黄县| 中阳县| 胶州市| 西盟| 蕲春县| 巴楚县| 定襄县| 南充市| 潼关县| 晋中市| 开封县| 崇文区| 康平县| 繁峙县| 枞阳县|