我不知道为什么以下函数适用于字符串ftp://ftp.hq.nasa.gov,但在ftp://ftp.hq.nasa.gov/incoming上打破
只是尝试获取传入文件的列表并相应地解析它们。必须有一些显而易见的东西。
我给了它在代码中进一步打破的界限。发布整个方法,以防万一。
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
//MessageBox.Show(reader.ReadToEnd());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
在下面一行打破
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
//MessageBox.Show(response.StatusDescription);
return result.ToString().Split('\n');
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
错误名称位于标题中:System.ArgumentOutOfRangeException: StartIndex cannot be less than zero
答案 0 :(得分:1)
在删除之前检查索引
int index = result.ToString().LastIndexOf('\n');
if(index >=0)
{
result.Remove(index, 1);
}