Link to home
Start Free TrialLog in
Avatar of Dovberman
DovbermanFlag for United States of America

asked on

Detect when end of file list is reached

I am reading each filename in a folder.

An error occurs when I am at the end of the list and try to read the next line.

Of course there is not a next line.

How can I stop reading lines when at the end of the list?

 strFileName = FolderReader.ReadLine();
        while (strFileName != null)
// OR While not at the end of the list
        {
            if (strFileName.ToString().IndexOf(".txt") > 0)
            {
                Uri UriFileTarget = new Uri("ftp://ftp.stockpicker.web704.discountasp.net/Data/" + strFileName);
                FtpWebRequest FileRequest = (FtpWebRequest)WebRequest.Create(UriFileTarget);
                FileRequest.Credentials = new NetworkCredential("0113718|stockpicker", "1088Delb");
                FileRequest.Method = WebRequestMethods.Ftp.DeleteFile;
                FtpWebResponse FileRequestResponse = (FtpWebResponse)FileRequest.GetResponse();
                strStatus = FileRequestResponse.StatusDescription;
                FileRequestResponse.Close();
            }
           
           If not on the last line (Syntax)
            strFileName = FolderReader.ReadLine(); // only if not at the end of the file list
        }
SOLUTION
Avatar of Roman Gherman
Roman Gherman
Flag of Moldova, Republic of 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
You are having this line in your code

 
FileRequestResponse.Close();

Open in new window



Just put this line out of loop.

Means when loop ends then do that. You are getting error because you are closing in loop and in next iteration you are again trying to read


Thanks,
Here is an example

 
String line;
try 
			{
				//Pass the file path and file name to the StreamReader constructor
				StreamReader sr = new StreamReader("C:\\Sample.txt");

				//Read the first line of text
				line = sr.ReadLine();

				//Continue to read until you reach end of file
				while (line != null) 
				{
					//write the lie to console window
					Console.WriteLine(line);
					//Read the next line
					line = sr.ReadLine();
				}

				//close the file
				sr.Close();
				Console.ReadLine();
			}
			catch(Exception e)
			{
				Console.WriteLine("Exception: " + e.Message);
			}
   finally 
			{
				Console.WriteLine("Executing finally block.");
			}

Open in new window


Reference: http://support.microsoft.com/kb/816149

Another Example

http://msdn.microsoft.com/en-us/library/aa287535(v=vs.71).aspx

These will not give you any error.

Thanks,
ASKER CERTIFIED SOLUTION
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
you can use this Concept

 
void Page_Load(object s, EventArgs e)
{
 DirectoryInfo di = new DirectoryInfo("c:/inetpub/wwwroot/demos");
 FileInfo[] rgFiles = di.GetFiles("*.txt");
 foreach(FileInfo fi in rgFiles)
 {
    Uri UriFileTarget = new Uri("ftp://ftp.stockpicker.web704.discountasp.net/Data/" + fi.FileName);
                FtpWebRequest FileRequest = (FtpWebRequest)WebRequest.Create(UriFileTarget);
                FileRequest.Credentials = new NetworkCredential("0113718|stockpicker", "1088Delb");
                FileRequest.Method = WebRequestMethods.Ftp.DeleteFile;
                FtpWebResponse FileRequestResponse = (FtpWebResponse)FileRequest.GetResponse();
                strStatus = FileRequestResponse.StatusDescription;
                FileRequestResponse.Close();

</a>");       
 }
}

Open in new window

Avatar of Dovberman

ASKER

I am experiencing a number of issues while trying to work with the remote FTP folder.

These are related to a new remote server.

I will attempt some solutions tomorrow and respond.

Thanks,
Solutions were workable. The remote server restricts programatic management of FTP folders.

I will revert to CuteFTP for FTP folder and file management.

Thanks for all your help.