如何将单元C:\的所有文件夹和子文件夹的列表转换为C#中的Xml文件 [英] How Do I Convert A List Of All Folders And Subfolders Of Unit C:\ To An Xml File In C#
本文介绍了如何将单元C:\的所有文件夹和子文件夹的列表转换为C#中的Xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我想在C#中创建一个小程序,允许加载包含任何单元的所有文件夹和子文件夹路径的列表,例如C:\,指向XML或任何其他文件类型。目标是在此之后,该文件将服务于另一个程序,我必须进行搜索并打开特定文件夹。
有可能这样做吗? br />
谢谢,
Hello,
I want to make a small program in C # that allows to load a list with the path of all folders and subfolders of any unit, eg "C: \", to an XML or any other file type. The goal is that after this, the file will serve to another program that I have to do a search and open particular folder.
It is possible to do this?
Thanks,
推荐答案
看看这里:如何:枚举目录和文件 [ ^ ]
现在,使用XDocument class [ ^ ]到将数据写入xml文件。
Have a look here: How to: Enumerate Directories and Files[^]
Now, use XDocument class[^] to write data to xml file.
string dirPath = @"E:\DOWNLOAD\";
var dirs = Directory.EnumerateDirectories(dirPath)
.Where(d=>d.Contains("codeproject"));
var files = dirs.SelectMany(d=>Directory.EnumerateFiles(d, "*.pdf", SearchOption.AllDirectories));
XDocument xdoc = new XDocument();
XElement xRoot = new XElement("Files");
foreach (var f in files)
{
xRoot.Add(new XElement("File", f));
}
xdoc.Add(xRoot);
xdoc.Save("FullFileNameAndPath.xml");
注意:这只是简单的样本,对于大量数据可能需要很长时间。
Note: it's just simple sample, for large amount of data it might take a long time.
这篇关于如何将单元C:\的所有文件夹和子文件夹的列表转换为C#中的Xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文