久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 C# 中遞歸上傳到 FTP 服務器

Recursive upload to FTP server in C#(在 C# 中遞歸上傳到 FTP 服務器)
本文介紹了在 C# 中遞歸上傳到 FTP 服務器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我需要通過 C# 代碼將一個文件夾(包含子文件夾和文件)從一臺服務器上傳到另一臺服務器.我做了一些研究,發(fā)現(xiàn)我們可以使用 FTP 來實現(xiàn)這一點.但是這樣我就只能移動文件而不是整個文件夾.任何幫助在這里表示贊賞.

I would need to upload a folder (which contains sub folders and files) from one server to another from C# code. I have done few research and found that we can achieve this using FTP. But with that I am able to move only files and not the entire folder. Any help here is appreciated.

推薦答案

FtpWebRequest(也不是 .NET 框架中的任何其他 FTP 客戶端)確實沒有任何明確支持遞歸文件操作(包括上傳).您必須自己實現(xiàn)遞歸:

The FtpWebRequest (nor any other FTP client in .NET framework) indeed does not have any explicit support for recursive file operations (including uploads). You have to implement the recursion yourself:

  • 列出本地目錄
  • 迭代條目、上傳文件并遞歸到子目錄(再次列出它們等)
void UploadFtpDirectory(string sourcePath, string url, NetworkCredential credentials)
{
    IEnumerable<string> files = Directory.EnumerateFiles(sourcePath);
    foreach (string file in files)
    {
        using (WebClient client = new WebClient())
        {
            Console.WriteLine($"Uploading {file}");
            client.Credentials = credentials;
            client.UploadFile(url + Path.GetFileName(file), file);
        }
    }

    IEnumerable<string> directories = Directory.EnumerateDirectories(sourcePath);
    foreach (string directory in directories)
    {
        string name = Path.GetFileName(directory);
        string directoryUrl = url + name;

        try
        {
            Console.WriteLine($"Creating {name}");
            FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create(directoryUrl);
            requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
            requestDir.Credentials = credentials;
            requestDir.GetResponse().Close();
        }
        catch (WebException ex)
        {
            FtpWebResponse response = (FtpWebResponse)ex.Response;
            if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
            {
                // probably exists already
            }
            else
            {
                throw;
            }
        }

        UploadFtpDirectory(directory, directoryUrl + "/", credentials);
    }
}

有關創(chuàng)建文件夾的復雜代碼的背景,請參閱:
如何檢查FTP目錄是否存在

使用如下函數(shù):

string sourcePath = @"C:sourcelocalpath";
// root path must exist
string url = "ftp://ftp.example.com/target/remote/path/";
NetworkCredential credentials = new NetworkCredential("username", "password");

UploadFtpDirectory(sourcePath, url, credentials);

一個更簡單的變體,如果您不需要遞歸上傳:
使用WebClient將文件目錄上傳到FTP服務器

A simpler variant, if you do not need a recursive upload:
Upload directory of files to FTP server using WebClient

或者使用可以自行進行遞歸的 FTP 庫.

Or use FTP library that can do the recursion on its own.

例如,使用 WinSCP .NET 程序集,您只需調用一次即可上傳整個目錄Session.PutFilesToDirectory:

For example with WinSCP .NET assembly you can upload whole directory with a single call to the Session.PutFilesToDirectory:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "ftp.example.com",
    UserName = "username",
    Password = "password",
};

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

    // Download files
    session.PutFilesToDirectory(@"C:sourcelocalpath", "/target/remote/path").Check();
}

Session.PutFilesToDirectory 方法默認是遞歸的.

The Session.PutFilesToDirectory method is recursive by default.

(我是 WinSCP 的作者)

這篇關于在 C# 中遞歸上傳到 FTP 服務器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發(fā)技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 精品国产乱码久久久久久蜜柚 | 2018天天干天天操 | 国内自拍视频在线观看 | 一级毛片色一级 | 欧美激情免费在线 | 日韩av一区二区在线观看 | 久久综合一区二区 | 国产一区二区三区四区三区四 | 亚洲视频一区在线 | 日韩中文字幕在线播放 | 欧洲一区二区视频 | 免费a网 | 国产日韩一区二区三免费高清 | 成人在线精品 | 一区二区三区四区国产 | 中文字幕在线观看一区 | 亚洲视频免费在线观看 | 久久av网站 | 日韩三级| 国产成人免费视频网站高清观看视频 | 成人av一区 | 久久激情网 | 亚洲国产一区在线 | 欧美一区二区三区在线 | 欧美成人h版在线观看 | 国产午夜精品久久久久 | 日韩成人影院在线观看 | aaaa网站 | 国产成人免费视频网站视频社区 | 91免费观看国产 | 中文字幕 在线观看 | 蜜桃视频在线观看免费视频网站www | 国产在线观看一区二区三区 | 国产视频h| 成人超碰| 91在线看网站| 日韩综合在线播放 | 色婷婷综合久久久中字幕精品久久 | 精品国产一区二区三区久久久蜜月 | 日韩精品1区2区 | 日本在线中文 |