Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i get listed files having extension .txt files

My code will fetch all files in current directry
C#
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://");
       
        Request.Method = WebRequestMethods.Ftp.ListDirectory;
        Request.Credentials = new NetworkCredential("user", "pass");
        FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
        
        Stream ResponseStream = Response.GetResponseStream();
        StreamReader Reader = new StreamReader(ResponseStream);
        
        //FileInfo[] Files = directoru.GetFiles("*.txt");

        ListBox1.Items.Add(Response.WelcomeMessage);
       
        while (!Reader.EndOfStream)//Read file name   
        {
            ListBox1.Items.Add(Reader.ReadLine().ToString());
        }
        Response.Close();
        ResponseStream.Close();
        Reader.Close();
Posted
Updated 25-Oct-13 0:36am
v3
Comments
ZurdoDev 25-Oct-13 7:34am    
Well, one easy way is if you can get the filename then check for .txt.
joshrduncan2012 25-Oct-13 9:17am    
This line:

//FileInfo[] Files = directoru.GetFiles("*.txt");

should work if you change it to:
Directory.GetFiles("*.txt");

1 solution

Here is how I did it in my application. My application is in VB .NET so I converted it to C# for you using Developer Fusion's VB .NET to C# converter. This example builds a collection named colFTPDirectory that contains the names of all of the .TXT files in the FTP directory along with their creation date and size.

C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Net;

...


bool bOK = false;
FtpWebRequest request = null;
FtpWebResponse response = null;
Stream responseStream = null;
StreamReader reader = null;
string strFilename = null;
System.DateTime dtCreated = null;
int intSize = 0;
string strTemp = null;
System.Collections.Generic.List<ftpdirectoryinfo> colFTPDirectory = null;

try {
    bOK = false;
    LogIt("Retrieving directory listing from " + strServerName + "/" + strFolder);
    // Get the object used to communicate with the server.
    request = (FtpWebRequest)WebRequest.Create("ftp://" + strServerName + "/" + strFolder);
    request.Timeout = 60000;
    request.ReadWriteTimeout = 60000;
    request.Credentials = new NetworkCredential(strUsername, strPassword);
    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    response = (FtpWebResponse)request.GetResponse();
    LogIt(response.BannerMessage);
    LogIt(response.WelcomeMessage);
    responseStream = response.GetResponseStream();
    reader = new StreamReader(responseStream);
    colFTPDirectory = new System.Collections.Generic.List<FTPDirectoryInfo>();
    while (!reader.EndOfStream) {
        strTemp = Convert.ToString(reader.ReadLine());
        dtCreated = Convert.ToDateTime(strTemp.Substring(0, 20).Trim);
        intSize = Convert.ToInt32(strTemp.Substring(20, 18).Trim);
        strFilename = strTemp.Substring(39).Trim;
        if (Path.GetExtension(strFilename).ToLower == ".txt") {
            colFTPDirectory.Add(new FTPDirectoryInfo(strFilename, dtCreated, intSize));
            //Debug.WriteLine(strTemp)
        }
    }
    bOK = true;
} catch (Exception ex) {
    LogIt("ERROR: " + ex.Message);
} finally {
    try {
        reader.Close();
    } catch {
    }
    try {
        response.Close();
    } catch {
    }
    reader = null;
    response = null;
}


...



internal class FTPDirectoryInfo
{
	private string m_filename;
	private System.DateTime m_CreationDate;
	private int m_size;
	public FTPDirectoryInfo(string Filename, System.DateTime CreationDate, int Size)
	{
		m_filename = Filename;
		m_CreationDate = CreationDate;
		m_size = Size;
	}
	public System.DateTime CreationDate {
		get { return m_CreationDate; }
	}
	public string Filename {
		get { return m_filename; }
	}
	public int Size {
		get { return m_size; }
	}
}
 
Share this answer
 
v4
Comments
Muhammad Amman 26-Oct-13 10:07am    
Mike Meinz how i embedd this code in .net ? i dont understand my application is web application please guide me thanks
Mike Meinz 26-Oct-13 10:59am    
This is C# .NET code. You can copy and paste it into the correct place in your source code.
Muhammad Amman 28-Oct-13 2:22am    
You mean i just copies into my aspx.cs file ?
Mike Meinz 28-Oct-13 6:39am    
That would be a good idea. You will have to put it into a subroutine and make any changes that are required to perform properly in your application. This code works in my application so all you need to do is put it in your application and make any necessary adjustments.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900