Dovberman
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().In dexOf(".tx t") > 0)
{
Uri UriFileTarget = new Uri("ftp://ftp.stockpicker.web704.discountasp.net/Data/" + strFileName);
FtpWebRequest FileRequest = (FtpWebRequest)WebRequest. Create(Uri FileTarget );
FileRequest.Credentials = new NetworkCredential("0113718 |stockpick er", "1088Delb");
FileRequest.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse FileRequestResponse = (FtpWebResponse)FileReques t.GetRespo nse();
strStatus = FileRequestResponse.Status Descriptio n;
FileRequestResponse.Close( );
}
If not on the last line (Syntax)
strFileName = FolderReader.ReadLine(); // only if not at the end of the file list
}
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().In
{
Uri UriFileTarget = new Uri("ftp://ftp.stockpicker.web704.discountasp.net/Data/" + strFileName);
FtpWebRequest FileRequest = (FtpWebRequest)WebRequest.
FileRequest.Credentials = new NetworkCredential("0113718
FileRequest.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse FileRequestResponse = (FtpWebResponse)FileReques
strStatus = FileRequestResponse.Status
FileRequestResponse.Close(
}
If not on the last line (Syntax)
strFileName = FolderReader.ReadLine(); // only if not at the end of the file list
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is an example
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,
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.");
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>");
}
}
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,
These are related to a new remote server.
I will attempt some solutions tomorrow and respond.
Thanks,
ASKER
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.
I will revert to CuteFTP for FTP folder and file management.
Thanks for all your help.
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,