久久久久久久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 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 欧美精品一区在线 | 国产精品精品视频一区二区三区 | 色综合久| 午夜久久久 | 欧洲尺码日本国产精品 | 一区二区三区在线 | 成人高清在线视频 | 不用播放器看的av | 四虎影视在线 | 亚洲 中文 欧美 日韩 在线观看 | 亚洲精品中文字幕在线观看 | 91精品国产日韩91久久久久久 | 亚洲欧美在线免费观看 | 亚洲综合国产精品 | 欧美久操网 | 亚洲成年在线 | 亚洲精品91 | 成人深夜福利 | 一区二区免费视频 | 国产在线高清 | 不卡的av在线 | 日韩黄| 视频一区二区国产 | 欧美日产国产成人免费图片 | 精品国产免费人成在线观看 | 欧美炮房 | 久久精品国产一区二区三区不卡 | 成人精品在线 | 青娱乐一区二区 | 一区二区日本 | 亚洲在线免费观看 | www.久久.com| 国产最新视频在线 | 狠狠操你 | 色综合视频| 精品精品| 国产成人精品免费视频 | 免费在线观看一级毛片 | 波多野结衣一区二区三区在线观看 | 国产精品久久久久久久久大全 | 精品久 |