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

C# Webclient Stream 將文件從 FTP 下載到本地存儲

C# Webclient Stream download file from FTP to local storage(C# Webclient Stream 將文件從 FTP 下載到本地存儲)
本文介紹了C# Webclient Stream 將文件從 FTP 下載到本地存儲的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我一直通過 .NET 命名空間提供的 WebClient 對象從 FTP 服務器下載文件,然后通過 將字節(jié)寫入實際文件>BinaryWriter.一切都很好.但是,現(xiàn)在,文件的大小急劇增加,我擔心內(nèi)存限制,所以我想創(chuàng)建一個下載流,創(chuàng)建一個文件流,并逐行從下載中讀取并寫入文件.

I've been downloading files from an FTP server via the WebClient object that the .NET namespace provides and then write the bytes to a actual file via a BinaryWriter. All is good. However, now, the files have dramatically increased in size and I'm worried about memory constraints so I'd like to create a download stream, create an file stream, and line by line read from the download and write to the file.

我很緊張,因為我找不到一個很好的例子.這是我的最終結(jié)果:

I'm nervous since I couldn't find a nice example of this. Here's my end result:

var request = new WebClient();

// Omitted code to add credentials, etc..

var downloadStream = new StreamReader(request.OpenRead(ftpFilePathUri.ToString()));
using (var writeStream = File.Open(toLocation, FileMode.CreateNew))
{
    using (var writer = new StreamWriter(writeStream))
    {
        while (!downloadStream.EndOfStream)
        {
            writer.Write(downloadStream.ReadLine());                  
        }
    }
}

我是否會采用這種不正確/更好的方式/等等?

Am I going about this incorrect/better way/etc?

推薦答案

你試過WebClient類的如下用法嗎?

Have you tried the following usage of WebClient class?

using (WebClient webClient = new WebClient())
{
    webClient.DownloadFile("url", "filePath");
}

更新

using (var client = new WebClient())
using (var stream = client.OpenRead("..."))
using (var file = File.Create("..."))
{
    stream.CopyTo(file);
}

如果您想使用自定義緩沖區(qū)大小顯式下載文件:

If you want to download file explicitly using customized buffer size:

public static void DownloadFile(Uri address, string filePath)
{
    using (var client = new WebClient())
    using (var stream = client.OpenRead(address))
    using (var file = File.Create(filePath))
    {
        var buffer = new byte[4096];
        int bytesReceived;
        while ((bytesReceived = stream.Read(buffer, 0, buffer.Length)) != 0)
        {
            file.Write(buffer, 0, bytesReceived);
        }
    }
}

這篇關(guān)于C# Webclient Stream 將文件從 FTP 下載到本地存儲的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 進行身份驗證并跨請求保留自定義聲明)
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)不起作用)
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ā)技
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調(diào)用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應用程序密碼從 oauth2/token 獲取訪問令牌)
New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 国产精品国产三级国产专区52 | 国产欧美日韩综合精品 | 日韩精品国产一区 | 精品欧美在线 | 黄视频在线播放 | 免费一区二区三区 | 欧美日韩精品一区 | 欧美第一页 | 日韩午夜激情 | 亚洲成人精品 | 亚洲欧美久久 | 五月天激情综合网 | 日本高清网站 | 国产成人三级一区二区在线观看一 | 国产三级视频在线 | 国产区在线视频 | 亚洲国产黄色 | 亚洲天堂国产 | 视频一区在线观看 | 婷婷激情六月 | 日本黄色中文字幕 | 国产精品美女视频 | 久久久国产一区二区三区 | 日本不卡在线 | 国产黄色在线播放 | 亚洲性色av| 精品福利在线 | 99精品成人 | 亚洲综合伊人 | 国产777| 五月婷婷网站 | 中文字幕在线一区二区三区 | 久久人人爱 | 日韩亚洲一区二区 | 午夜免费在线 | 亚洲精品久久 | 中文字幕免费 | 久久久国产视频 | 欧美在线观看一区 | 一个色综合网 | 国产精品高清在线观看 |