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

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

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

問題描述

限時送ChatGPT賬號..

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

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 客戶端)確實沒有任何明確支持遞歸文件操作(包括上傳).您必須自己實現遞歸:

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);
    }
}

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

使用如下函數:

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模板網!

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

相關文檔推薦

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屋-程序員軟件開發技
.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 超時)
主站蜘蛛池模板: 欧美91| 精品在线免费观看 | 免费性视频| 久久免费小视频 | 国产精品免费一区二区 | 日韩av不卡在线观看 | 欧美日韩色| 日韩一级av毛片 | 在线免费黄色网址 | 亚洲高清毛片一区二区 | 日韩在线免费视频 | 午夜男人影院 | 欧美日韩成人在线观看 | 黄网在线播放 | 96av视频 | 在线观看中文字幕 | 一级片在线视频 | 午夜小视频在线观看 | 国产精品理论 | 久热伊人| 伊人成人在线视频 | 中文字幕在线一区二区三区 | 夜夜骑夜夜 | 午夜免费小视频 | 久久精品99 | 色中色av| 欧美日韩一区二区在线观看 | 超碰在线99 | 日韩免费一区二区三区 | 青青草国产在线视频 | 国产最新视频 | 日韩成人在线免费观看 | av青青草原 | 极品在线视频 | 欧美精品区 | 在线色 | 久热在线视频 | 在线免费毛片 | 精品福利一区 | 日韩成人精品一区二区 | 亚洲午夜激情 |