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
        }
DovbermanAsked:
Who is Participating?
 
khan_webguruCommented:
Sorry @Alex500 I miss read your problem I thought for text reading from Txt.
0
 
Roman GhermanSenior Software EngineerCommented:
while ((strFileName = FolderReader.ReadLine()) != null)
        {
            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();
            }
       }
0
 
khan_webguruCommented:
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,
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
khan_webguruCommented:
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,
0
 
khan_webguruCommented:
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

0
 
DovbermanAuthor Commented:
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,
0
 
DovbermanAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.