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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

khan_webguruCommented:
Sorry @Alex500 I miss read your problem I thought for text reading from Txt.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.