本文介绍了与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();
推荐答案
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连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!