C#Ftp错误 - 空引用

时间:2014-11-22 19:27:55

标签: c# ftp

我有这个代码,当FTP站点上有文件时可以正常工作,但是当FTP站点上没有文件时,它会失败。 错误II get在 foreach(文件中的字符串文件)中,它表示存在空引用。

如何解决此问题,以便如果FTP站点上没有文件,则此代码将起作用。

提前致谢。

我的错误讯息 System.NullReferenceException未被用户代码

处理

代码

public void 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");
            }

        }

2 个答案:

答案 0 :(得分:1)

你需要为null测试GetFilesList()的结果,因为如果出现错误(在GetFileList()的catch子句中你将结果 - downloadFiles - 设置为null,则返回null。

        public void Main() 
        {               
            String[] files = GetFileList();
            if (files != null)  // add this line
            {
                foreach (string file in files)
                {
                    Download(file);
                }
            }

答案 1 :(得分:0)

问题是你对GetFileList的调用返回null,因此foreach失败。