gpt4 book ai didi

C# 使用 FTP 服务器目录填充 TreeView

转载 作者:太空宇宙 更新时间:2023-11-03 20:12:58 24 4
gpt4 key购买 nike

我已经在网上搜索过,但对这个还是陌生的。请耐心等待我。

我想做的是:- 我有一个名为“treeTO”的 TreeView。- 我连接到我的 FTP 服务器,我想在 TreeView 中填充此处的所有内容(目录、子目录和文件,无论它是什么)。- 这是我到目前为止的代码:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ActiveServer.Server + "/");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(ActiveServer.UserName, ActiveServer.Password);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

-这会返回以下文本:Logs、Data 和 WwwRoot。

我很难将其添加到 TreeView ,然后遍历文件夹以将所有内容添加到 TreeView 。

最佳答案

所以这是我为您提供的有效解决方案!

当然,您必须将“root”的值替换为您的 FTP 服务器的 rootURL,并在方法 GetWebRequest() 中将“用户名”和“密码”替换为您的凭据!

你需要这个类来保存细节

public class FTPListDetail
{
public bool IsDirectory
{
get
{
return !string.IsNullOrWhiteSpace(Dir) && Dir.ToLower().Equals("d");
}
}
internal string Dir { get; set; }
public string Permission { get; set; }
public string Filecode { get; set; }
public string Owner { get; set; }
public string Group { get; set; }
public string Name { get; set; }
public string FullPath { get; set; }
}

这是您的 WinForm 应用程序的代码隐藏

private void button1_Click(object sender, EventArgs e)
{
var root = "ftp://ftp.yourFTPServer.at";

treeView1.Nodes.Clear();
treeView1.Nodes.Add(CreateDirectoryNode(root, "root"));
}

private TreeNode CreateDirectoryNode(string path, string name)
{
var directoryNode = new TreeNode(name);
var directoryListing = GetDirectoryListing(path);

var directories = directoryListing.Where(d => d.IsDirectory);
var files = directoryListing.Where(d => !d.IsDirectory);

foreach (var dir in directories)
{
directoryNode.Nodes.Add(CreateDirectoryNode(dir.FullPath, dir.Name));
}
foreach (var file in files)
{
directoryNode.Nodes.Add(new TreeNode(file.Name));
}
return directoryNode;
}

public IEnumerable<FTPListDetail> GetDirectoryListing(string rootUri)
{
var CurrentRemoteDirectory = rootUri;
var result = new StringBuilder();
var request = GetWebRequest(WebRequestMethods.Ftp.ListDirectoryDetails, CurrentRemoteDirectory);
using (var response = request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
if (string.IsNullOrEmpty(result.ToString()))
{
return new List<FTPListDetail>();
}
result.Remove(result.ToString().LastIndexOf("\n"), 1);
var results = result.ToString().Split('\n');
string regex =
@"^" + //# Start of line
@"(?<dir>[\-ld])" + //# File size
@"(?<permission>[\-rwx]{9})" + //# Whitespace \n
@"\s+" + //# Whitespace \n
@"(?<filecode>\d+)" +
@"\s+" + //# Whitespace \n
@"(?<owner>\w+)" +
@"\s+" + //# Whitespace \n
@"(?<group>\w+)" +
@"\s+" + //# Whitespace \n
@"(?<size>\d+)" +
@"\s+" + //# Whitespace \n
@"(?<month>\w{3})" + //# Month (3 letters) \n
@"\s+" + //# Whitespace \n
@"(?<day>\d{1,2})" + //# Day (1 or 2 digits) \n
@"\s+" + //# Whitespace \n
@"(?<timeyear>[\d:]{4,5})" + //# Time or year \n
@"\s+" + //# Whitespace \n
@"(?<filename>(.*))" + //# Filename \n
@"$"; //# End of line

var myresult = new List<FTPListDetail>();
foreach (var parsed in results)
{
var split = new Regex(regex)
.Match(parsed);
var dir = split.Groups["dir"].ToString();
var permission = split.Groups["permission"].ToString();
var filecode = split.Groups["filecode"].ToString();
var owner = split.Groups["owner"].ToString();
var group = split.Groups["group"].ToString();
var filename = split.Groups["filename"].ToString();
myresult.Add(new FTPListDetail()
{
Dir = dir,
Filecode = filecode,
Group = group,
FullPath = CurrentRemoteDirectory + "/" + filename,
Name = filename,
Owner = owner,
Permission = permission,
});
};
return myresult;
}
}
}

private FtpWebRequest GetWebRequest(string method, string uri)
{
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return null;
}
var reqFTP = (FtpWebRequest)FtpWebRequest.Create(serverUri);
reqFTP.Method = method;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("yourUser", "yourPassword");
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
return reqFTP;
}

关于C# 使用 FTP 服务器目录填充 TreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18830900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com