簡體   English   中英

從FTP下載時如何從流中讀取/寫入以null結尾的字符串

[英]How to read / write a null terminated string from stream while downloading from FTP

我正在使用一個Updater,該Updater下載一個updatelist.xml文件,該文件包含有關FTP服務器上文件的信息(哈希碼,文件名和文件目錄)。

然后,它獲取本地計算機上這些文件的信息,如果它們不同(基於哈希碼),則更新程序將繼續從ftp服務器下載更新的文件。

效果很好,但是當涉及到txt文件時,我遇到了一個小問題。 基本上,當我編寫文件時,它不會寫以null結尾的字符串,例如:

這是FTP服務器上的文件:

"Come Lads! How does things go?
I see my youth in you and it makes

將文件下載到本地計算機后,它就是同一文件:

"Come Lads! How does things go?I see my youth in you and it makes

我一直在互聯網上搜索信息,但不了解如何解決此問題。

這是代碼的一部分,我認為這是重要的部分。

FileStream file;
file = File.Open(downloadFilePath, FileMode.Create);

byte[] buffer = new byte[1024 * 30];

int read;
long downloaded = 0;
while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    downloaded += read;

    string text = string.Format("Descargando archivo {0} de {1}: {2} - {3} / {4}", curFiles, totalFiles,
        xn["Name"].InnerText, BytesToString(downloaded), BytesToString(contentLength));

    label_progreso.Text = text;

    progressBar2.Value = (int)ProgressBarValue(downloaded, contentLength);

    file.Write(buffer, 0, read);
}

顯然,問題在於,當程序創建並寫入文件時,它不會寫入以null結尾的字符串。

我缺少什么?

我已經使用webclient.download而不是FtpWebRequest找到了信息,我已經嘗試過了,但是它也不起作用。

您應該始終關閉FileStream對象。 嘗試使用語句

using(FileStream file = File.Open(downloadFilePath, FileMode.Create))
{
    //code
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM