StreamReader ReadToEnd() 对于不同服务器上的 txt 文件给出不同的结果

发布于 2024-11-28 09:48:19 字数 1031 浏览 0 评论 0 原文

我创建了一个小型 FTP 程序,仅供我自己使用,因此登录详细信息 + 文件路径是硬编码的。

我有一个按钮,可以启动两个 txt 文件的下载过程 - 这些文件的内容被放入两个不同的文本框中。

txt 文件使用 UTF-8 编码,如下所示:

line1
line2
line3
etc.

我已将这两个文件放在两个不同的服务器上(每个服务器上有两个文件)。在服务器 1 上,两个文件均已下载并正确显示在文本框中,如下所示:

line1
line2
line3
etc.

在服务器 2 上,两个文件均已下载并显示在文本框中,如下所示:

line1line2line3etc.

我真的不明白为什么 - 我编辑了软件(下载过程)也没有编辑文件,当然,由于服务器的变化,我只编辑了硬编码的文件路径。

这就是我下载其中一个文件的方式(另一个文件的方式相同,只是名称不同):

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(folder + artistsFileNameTxt);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(login, pass);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

tbxArtists.Text = reader.ReadToEnd();

reader.Close();
response.Close();

有帮助吗?

I have created a small FTP program, it's just for my own use, so login details + the file paths are hard coded.

I have a button which starts the downloading process of two txt files - the contents of these are put into two different textboxes.

The txt files are encoded with UTF-8, and look like this:

line1
line2
line3
etc.

I have placed these two files on two different servers (two files on each server). On server 1, both files are downloaded and shown in the textboxes correctly, like this:

line1
line2
line3
etc.

On server 2, both files are downloaded and shown in the textboxes like this:

line1line2line3etc.

I really don't understand why - I have not edited the software (the downloading process) nor the files, I have only edited the hard coded file paths of course, because of the change of server.

This is how I download one of the files (the other file is the same way, just with different names):

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(folder + artistsFileNameTxt);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(login, pass);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

tbxArtists.Text = reader.ReadToEnd();

reader.Close();
response.Close();

Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我恋#小黄人 2024-12-05 09:48:19

尝试

request.UseBinary = false;

默认值为 true...仅当您确定正在处理文本文件时才执行此操作。

FTP 协议具有此“内置”功能来处理有关 NewLine 的系统差异...

顺便说一句,通过 FTP 上传时也必须正确设置此设置,否则可能会变得混乱...

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usebinary.aspx
http://www.rhinosoft.com/newsletter/NewsL2008-03-18.asp

try

request.UseBinary = false;

the default is true... do this ONLY when you are SURE that you are dealing with a text file.

FTP protocol has this "built-in" to deal with system-differences regardings NewLine...

BTW you must set this setting correctly when uploading via FTP too otherwise it can get messy...

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usebinary.aspx
http://www.rhinosoft.com/newsletter/NewsL2008-03-18.asp

迎风吟唱 2024-12-05 09:48:19

我的猜测是第一个服务器是Windows,而第二个服务器是Linux。
Windows 新行: \r\n
Linux 新行: \n
在 Windows 中无法正确显示

My guess is that the first server is windows, whereas the second one is linux.
Windows new line: \r\n
Linux new line: \n
is not displayed correctly in windows

满栀 2024-12-05 09:48:19

尝试

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(folder + artistsFileNameTxt);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(login, pass);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);

tbxArtists.Text = reader.ReadToEnd();

reader.Close();
response.Close();

try

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(folder + artistsFileNameTxt);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(login, pass);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);

tbxArtists.Text = reader.ReadToEnd();

reader.Close();
response.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文