C#Ftp错误-空引用

C#Ftp错误-空引用,c#,ftp,C#,Ftp,我有这段代码,当FTP站点上有文件时,它工作得非常完美,但当FTP站点上没有文件时,它会失败。 错误II get在foreach(文件中的字符串文件)中,它表示存在空引用 如何修复此问题,以便在FTP站点上没有文件时,此代码可以正常工作 提前谢谢 我的错误消息 用户代码未处理System.NullReferenceException 代码 公共图书馆 { String[] files = GetFileList(); foreach (string

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

如何修复此问题,以便在FTP站点上没有文件时,此代码可以正常工作

提前谢谢

我的错误消息 用户代码未处理System.NullReferenceException

代码

公共图书馆 {

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

        }

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


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

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