与C#的FTP连接 [英] FTP CONNECTION WITH C#

查看:112
本文介绍了与C#的FTP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何列出具有扩展名 .txt 的文件



我的代码将获取当前目标中的所有文件

How can i get listed files having extension .txt files

My code will fetch all files in current directry

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();

推荐答案

以下是我在应用程序中的操作方法。我的应用程序是在VB .NET中,所以我使用Developer Fusion的VB .NET到C#转换器将它转换为C#。此示例构建一个名为 colFTPDirectory 的集合,其中包含FTP目录中所有.TXT文件的名称及其创建日期和大小。



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.

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; }
	}
}


这篇关于与C#的FTP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆