我怎么能和其他人谁在读这份名单从在线目录中的所有文件到ListView?
这是代码的本地目录中列出我想知道是否有办法让这个是连接到FTP网站,并列出文件?
FolderBrowserDialog folderPicker = new FolderBrowserDialog();
if (folderPicker.ShowDialog() == DialogResult.OK)
{
ListView1.Items.Clear();
string[] files = Directory.GetFiles(folderPicker.SelectedPath);
foreach (string file in files)
{
string fileName = Path.GetFileNameWithoutExtension(file);
ListViewItem item = new ListViewItem(fileName);
item.Tag = file;
ListView1.Items.Add(item);
}
}
我已经使用这个代码,但我似乎无法得到它的工作它不是来了一个错误,但它不是上市的web服务器上的文件要么?
private void ConnectBtn_Click(object sender, EventArgs e)
{
ListDirectory();
}
public string[] ListDirectory()
{
var list = new List<string>();
var request = createRequest(TxtServer.Text, WebRequestMethods.Ftp.ListDirectory);
using (var response = (FtpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream, true))
{
while (!reader.EndOfStream)
{
list.Add(reader.ReadLine());
}
}
}
}
return list.ToArray();
}
private FtpWebRequest createRequest(string uri, string method)
{
var r = (FtpWebRequest)WebRequest.Create(uri);
r.Credentials = new NetworkCredential(TxtUsername.Text, TxtPassword.Text);
r.Method = method;
return r;
}
你可以使用这个包装库 。
相关的代码是:
public string[] ListDirectory() {
var list = new List<string>();
var request = createRequest(WebRequestMethods.Ftp.ListDirectory);
using (var response = (FtpWebResponse)request.GetResponse()) {
using (var stream = response.GetResponseStream()) {
using (var reader = new StreamReader(stream, true)) {
while (!reader.EndOfStream) {
list.Add(reader.ReadLine());
}
}
}
}
return list.ToArray();
}
这里是一个很好的帮手得到一个FTP目录下的所有文件和文件夹:
public static IEnumerable<string> GetFilesInFtpDirectory(string url, string username, string password)
{
// Get the object used to communicate with the server.
var request = (FtpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(username,password);
using (var response = (FtpWebResponse) request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
var reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (string.IsNullOrWhiteSpace(line) == false)
{
yield return line.Split(new[] { ' ', '\t' }).Last();
}
}
}
}
}
这里找到: http://www.snippetsource.net/Snippet/128/get-all-files-of-an-ftp-directory-in-c
我找到了答案,我做了一个小实验,现在它显示在列表视图中的文件,谢谢罗伯特·哈维♦
private void ConnectBtn_Click(object sender, EventArgs e)
{
ListDirectory();
}
public string[] ListDirectory()
{
var list = listView1;
var request = createRequest(TxtServer.Text, WebRequestMethods.Ftp.ListDirectory);
using (var response = (FtpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream, true))
{
while (!reader.EndOfStream)
{
list.Items.Add(reader.ReadLine());
}
}
}
} List<string> l = new List<string>();
return l.ToArray();
}
private FtpWebRequest createRequest(string uri, string method)
{
var r = (FtpWebRequest)WebRequest.Create(uri);
r.Credentials = new NetworkCredential(TxtUsername.Text, TxtPassword.Text);
r.Method = method;
return r;
}
该代码可用于从FTP获得的文件列表
private void ftpFileRead()
{
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("your ftpAddress");
Request.Method = WebRequestMethods.Ftp.ListDirectory;
Request.Credentials = new NetworkCredential(your ftp username,your ftp password);
FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
Stream ResponseStream = Response.GetResponseStream();
StreamReader Reader = new StreamReader(ResponseStream);
ListBox1.Items.Add(Response.WelcomeMessage);
while (!Reader.EndOfStream)//Read file name
{
ListBox1.Items.Add(Reader.ReadLine().ToString());
}
Response.Close();
ResponseStream.Close();
Reader.Close();
}
您可以使用此方法。
public static string[] GetFiles(string path, NetworkCredential Credentials, SearchOption searchOption)
{
var request = (FtpWebRequest)WebRequest.Create(path);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = Credentials;
List<string> files = new List<string>();
using (var response = (FtpWebResponse)request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
var reader = new System.IO.StreamReader(responseStream);
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (string.IsNullOrWhiteSpace(line) == false)
{
if (!line.Contains("<DIR>"))
{
string[] details = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
string file = line.Replace(details[0], "")
.Replace(details[1], "")
.Replace(details[2], "")
.Trim();
files.Add(file);
}
else
{
if (searchOption == SearchOption.AllDirectories)
{
string dirName = line.Split(
new string[] { "<DIR>" },
StringSplitOptions.RemoveEmptyEntries
).Last().Trim();
string dirFullName = String.Format("{0}/{1}", path.Trim('/'), dirName);
files.AddRange(GetFiles(dirFullName, Credentials, searchOption));
}
}
}
}
}
}
return files.ToArray();
}