C# FTP 错误 - 空引用
本文关键字:引用 错误 FTP | 更新日期: 2023-09-27 18:31:28
我有这段代码,当FTP站点上有文件时,它可以完美地工作,但是当FTP站点上没有文件时,它会失败。错误 II 得到是在 foreach 中(文件中的字符串文件),它说有空引用。
我该如何解决此问题,以便在FTP站点上没有文件时,此代码将起作用。
提前谢谢。
我的错误消息System.NullReferenceException 未由用户代码处理
法典
公共无效 Main() {
String[] files = GetFileList();
foreach (string file in files)
{
Download(file);
}
}
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
WebResponse response = null;
StreamReader reader = null;
try
{
//FtpWebRequest reqFTP;
WebRequest reqFTP;
reqFTP = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + Dts.Variables["strHost"].Value+"/"));
//reqFTP.UseBinary = true;
String FTPUser = (String)Dts.Variables["strUserName"].Value;
String FTPPwd = (String)Dts.Variables["strPassword"].Value;
reqFTP.Credentials = new NetworkCredential(FTPUser, FTPPwd);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Proxy = null;
//reqFTP.KeepAlive = true;
//reqFTP.UsePassive = true;
response = reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("'n");
line = reader.ReadLine();
}
// to remove the trailing ''n'
result.Remove(result.ToString().LastIndexOf(''n'), 1);
return result.ToString().Split(''n');
}
catch (Exception ex)
{
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
downloadFiles = null;
return downloadFiles;
}
}
private void Download(string file)
{
try
{
string uri = "ftp://" + Dts.Variables["strHost"].Value + "/" + file;
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return;
}
WebRequest reqFTP;
//FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + Dts.Variables["strHost"].Value + "/" + file));
String FTPUser = (String)Dts.Variables["strUserName"].Value;
String FTPPwd = (String)Dts.Variables["strPassword"].Value;
reqFTP.Credentials = new NetworkCredential(FTPUser, FTPPwd);
//reqFTP.KeepAlive = true;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
//reqFTP.UseBinary = true;
reqFTP.Proxy = null;
//reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writeStream = new FileStream(Dts.Variables["strLocalFolder"].Value + "''" + file, FileMode.Create); int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
writeStream.Close();
response.Close();
}
catch (WebException wEx)
{
MessageBox.Show(wEx.Message, "Download Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Download Error");
}
}
你需要测试 GetFilesList() 的结果是否为 null,因为如果出现错误,你会返回 null(在 GetFileList() 的 catch 子句中,你将结果 - downloadFiles - 设置为 null)。
public void Main()
{
String[] files = GetFileList();
if (files != null) // add this line
{
foreach (string file in files)
{
Download(file);
}
}
问题是你对 GetFileList 的调用返回 null,因此 foreach 失败。