Link to home
Start Free TrialLog in
Avatar of Abacus IT
Abacus ITFlag for United States of America

asked on

Streamreader Timing out

Hello Everyone,

I have a streamreader that is called to read a response back from an FTP Web Request. I need to keep the reader open the whole time I am processing some files I am downloading from the FTP site to a local directory. Because of the size of the files, I am able to pull 3 of them and process them, but before the 4th file processes, I get an error stating that:

The underlying connection was closed: An unexpected error occurred on a receive.

Digging into the error message in the locals window shows me the streamreader object threw an exception of type System.ObjecDisposedException.

This being said, the reader is timing out before I am finished with the files. Is there a way around this?
ASKER CERTIFIED SOLUTION
Avatar of WesWilson
WesWilson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Abacus IT

ASKER

This section of code is decently large for such a 'simple' process, but here it is. The basis for the code is that it will get a directory listing of a location on the FTP site, then for each file it finds, store the creation date and time, then process the file ( download the file and push it into SQL) before moving on to the next file from the listing.
private void auditTimer_Tick(object sender, EventArgs e)
        {
            Add.AddToFile("Audit Timer event.");
            auditTimer.Enabled = false;

            FTP.remotePath = GetSystemSettings("ftpPathAudit");
            OpenFNBO();
            string[] auditnameList = FTP.getFileList("*");
            
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://ftp.goabacus.com/$BIRCH.AC13110O");
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            ftpRequest.Credentials = new NetworkCredential("goabacusftp", "ftpaccessforme");
            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            Stream responseStream = ftpResponse.GetResponseStream();
            StreamReader responseReader = new StreamReader(responseStream);
            

            while (!responseReader.EndOfStream)
            {
                entryDetails = responseReader.ReadLine().Split(new char[] { ' ' }, 4, StringSplitOptions.RemoveEmptyEntries);

                DateTime modified = DateTime.Parse(entryDetails[0] + " " + entryDetails[1]);

                bool isdir = false;
                int size = -1;
                if (entryDetails[2] == "<DIR>")
                {
                    isdir = true;
                }
                else
                {
                    size = Int32.Parse(entryDetails[2]);
                }

                string name = entryDetails[3];
                ftpDate = entryDetails[0];
                ftpTime = entryDetails[1];

                Add.AddToFile(String.Format("Entry name: {0}\r\rIs directory: {1}\r\nFile Size: {2}\r\nModification Date/Time: {3}\r\n", name, isdir, size, modified));
            

                if (name.Length > 0)
                {
                    
                        splitNameAudit = name;
                        string localAuditName = "C:/FTPFiles/Audit/ " + name.Trim();
                        if (name.Length > 0)
                        {
                            FTP.download(splitNameAudit, localAuditName, true);
                            if (FTP.downloadSuccess == true)
                            {
                                FTP.deleteRemoteFile(splitNameAudit);
                            }
                            File.Move(@"C:\FTPFiles\Audit\ " + name.Trim(), @"C:\FTPFiles\Audit\ " + name.Trim() + ".tar");
                            auditFileName = "ACII_AUDIT.tar";
                            audit = name.Trim() + ".tar";
                    
                            try
                            {
                                string dirpath = @"C:\FTPFiles\Audit\ ";
                                DirectoryInfo di = new DirectoryInfo(dirpath);
                                foreach (FileInfo fi in di.GetFiles("*"))
                                {
                                    Decompress(fi);
                                    Extract(auditFileName);
                                  
                                    Directory.SetCurrentDirectory(@"C:\FTPFiles\Audit\usr2\mapp\mappief\runtime\tmp\ ");
                                    DirectoryInfo extract = new DirectoryInfo(@"C:\FTPFiles\Audit\usr2\mapp\mappief\runtime\tmp\ ");
                                    foreach (FileInfo extraction in extract.GetFiles("*"))
                                    {
                                        string toFile = @"C:\FTPFiles\Extracted\" + extraction.ToString().Substring(0, extraction.ToString().Length - 13) + "_" + ftpDate + "_" + ftpTime.Remove(2, 1);
                                        while (File.Exists(toFile))
                                        {
                                            toFile = toFile + "A";
                                        }
                                        File.Move(@"C:\FTPFiles\Audit\usr2\mapp\mappief\runtime\tmp\" + extraction, toFile);
                                    }
                                    SQLInsert(); 
                                    responseReader.Close();
                                    Directory.SetCurrentDirectory(@"C:\FTPFiles\Audit\ ");
                                    File.Move(@"C:\FTPFiles\Audit\ " + fi.Name.Trim(), @"C:\FTPFiles\Audited\ " + fi.Name.Trim() + ".processed_" + currentDateString());
                                    File.Delete("ACII_AUDIT.tar");
                                    Directory.Delete(@"C:\FTPFiles\Audit\usr2 ",true);
                                    
                                } 
                            }
                            catch (Exception ex)
                            {
                                Add.AddToFile(ex.Message.ToString());

                            }
                            //Update FTPImport table with appropriate data.
                            insertFTPImport(ftpDate, "N/A","Audit");
                        
                            }
                        
                    }
                }

                auditTimer.Enabled = true;
        }

Open in new window

can you just check anywhere you have put values in seconds instead of MILLI-SECONDS?
Please view new post for this question shortly.