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

在 C# 中遞歸上傳到 FTP 服務(wù)器

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

問題描述

限時送ChatGPT賬號..

我需要通過 C# 代碼將一個文件夾(包含子文件夾和文件)從一臺服務(wù)器上傳到另一臺服務(wù)器.我做了一些研究,發(fā)現(xiàn)我們可以使用 FTP 來實(shí)現(xiàn)這一點(diǎ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 客戶端)確實(shí)沒有任何明確支持遞歸文件操作(包括上傳).您必須自己實(shí)現(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);
    }
}

有關(guān)創(chuàng)建文件夾的復(fù)雜代碼的背景,請參閱:
如何檢查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服務(wù)器

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

或者使用可以自行進(jìn)行遞歸的 FTP 庫.

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

例如,使用 WinSCP .NET 程序集,您只需調(diào)用一次即可上傳整個目錄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 方法默認(rèn)是遞歸的.

The Session.PutFilesToDirectory method is recursive by default.

(我是 WinSCP 的作者)

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

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

相關(guān)文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進(jìn)行身份驗(yàn)證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權(quán)不起作用)
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(如何獲取守護(hù)進(jìn)程或服務(wù)器到 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(異步調(diào)用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 国产专区视频 | 日韩中字幕| aaa综合国产 | 欧美性久久久 | 天天在线操 | 久久久久久久国产 | 久久综合九九 | 久久av一区二区三区 | 亚洲精品一区在线 | 色狠狠一区| 91在线精品秘密一区二区 | 亚洲成人免费av | 国产高清免费 | 久久天堂 | 九九在线视频 | 亚洲高清在线观看 | 色狠狠桃花综合 | 亚洲精品一区二区网址 | 日本不卡高清视频 | 中文字幕免费在线 | 91精品国产综合久久久久久丝袜 | 欧美日韩久久精品 | 国产精品99久久久久久久久久久久 | 久久国产精品精品 | 亚洲综合色视频在线观看 | 久久99蜜桃综合影院免费观看 | 午夜在线视频 | 亚洲天天干 | 精品视频免费 | wwww.8888久久爱站网 | 三级av免费 | 特级特黄特色的免费大片 | 欧美激情一区二区三级高清视频 | 尹人av | 盗摄精品av一区二区三区 | 亚洲成人精品国产 | 亚洲一区二区中文字幕 | 韩日在线 | 成人国产精品色哟哟 | 99一级毛片 | 亚洲成人综合在线 |