Reputation: 5468
I am using the following code to fetch the output of FtpWebRequest
and then parse the lines one by one.
FTPEntity entity = new FTPEntity(entityName);
entities.Add(entity);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(textBoxFTPSite.Text + entityName);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(textBoxFTPUserName.Text, textBoxFTPPassword.Text);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string[] outputlines = reader.ReadToEnd().Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
foreach (string info in outputlines) {
if (info == "") {
worker.ReportProgress(1);
continue;
}
var tokens = info.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
Since there is nothing in that FTP directory, the output of ReadToEnd()
is blank. But still the Split function is creating an array of one blank element and I had to use an if
statement to filter that one out.
Why is Trim()
not trimming completely?
Upvotes: 1
Views: 322
Reputation: 2921
Assuming the output of your ReadToEnd()
call is an empty string (but not blank, as that is different) then the Trim()
method is doing its job perfectly, i.e. removing all white space, despite the fact that there isn't any.
The String.Split()
method will always return an array of at least one element, even if that element is an empty string. Since your string contains no text and is empty, you are getting an array of one empty string.
Upvotes: 1