久久久久久久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 服務器下載文件,然后通過 將字節寫入實際文件>BinaryWriter.一切都很好.但是,現在,文件的大小急劇增加,我擔心內存限制,所以我想創建一個下載流,創建一個文件流,并逐行從下載中讀取并寫入文件.

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.

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

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

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

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

這篇關于C# Webclient Stream 將文件從 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 令牌授權不起作用)
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屋-程序員軟件開發技
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 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 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 中文字幕一区二区三区精彩视频 | 亚洲最新网址 | 中文字幕一区二区三区乱码图片 | 亚洲第一免费播放区 | 国产精品乱码一区二区三区 | 欧美日韩在线综合 | 欧美成人免费 | 9999在线视频 | 国产精品久久久久婷婷二区次 | 九九九视频在线 | 精品无码久久久久久久动漫 | 久久爱综合 | 亚洲成人av | 国产精品欧美日韩 | 视频一区二区在线观看 | 欧美日韩精品 | 免费亚洲婷婷 | 国产在线一区二区 | 国产精品区二区三区日本 | 国产精品一区二区在线 | 国产午夜精品一区二区三区嫩草 | 国产高清一区二区三区 | 中文字幕综合在线 | 久久久久久免费毛片精品 | 国产成人a亚洲精品 | 2018中文字幕第一页 | 超碰操| 亚洲欧美在线视频 | 国产高清一区二区三区 | 天天玩夜夜操 | 亚洲欧美第一视频 | 日本不卡视频在线播放 | 国产精品久久久av | 米奇狠狠鲁 | 成人精品国产一区二区4080 | 在线免费激情视频 | 九九精品在线 | 欧美日韩一区二区三区不卡视频 | 成人综合伊人 | 97精品国产97久久久久久免费 | 日本在线免费 |