C# 将单元c:\的所有文件夹和子文件夹列表转换为c中的XML文件#

C# 将单元c:\的所有文件夹和子文件夹列表转换为c中的XML文件#,c#,xml,C#,Xml,我想用C#编写一个小程序,它可以将任何单元的所有文件夹和子文件夹的路径(例如“C:\”)加载到XML或任何其他文件类型。目标是,在此之后,该文件将服务于另一个程序,我必须进行搜索并打开特定文件夹 有可能做到这一点吗 谢谢,一些基本的指针(不,不是那些指针!!) 对于文件枚举,请查看:Directory.EnumerateDirectories(“C:/”) 对于Xml编写,请查看XmlTextWriter 试一试。当然,可以使用递归遍历文件夹结构。有了它,您可以在递归中将XmlNode作为参数传

我想用C#编写一个小程序,它可以将任何单元的所有文件夹和子文件夹的路径(例如“C:\”)加载到XML或任何其他文件类型。目标是,在此之后,该文件将服务于另一个程序,我必须进行搜索并打开特定文件夹

有可能做到这一点吗

谢谢,

一些基本的指针(不,不是那些指针!!)

对于文件枚举,请查看:Directory.EnumerateDirectories(“C:/”)

对于Xml编写,请查看XmlTextWriter


试一试。

当然,可以使用递归遍历文件夹结构。有了它,您可以在递归中将XmlNode作为参数传递,以便添加新的子节点(每个子文件夹都是子节点,在递归方法中依此类推)

下面是递归的一个示例。以它为基础

private static void FolderStructure(string path)
{
  Console.WriteLine(path);
  foreach (string item in System.IO.Directory.GetDirectories(path))
  {
    FolderStructure(item);
  }
}

FolderStructure(@"X:\\My\\Path");

问候

我决定执行另一个程序来测试XML文件的创建。工作正常,文件创建正确。 代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Xml.Linq;

namespace generate_XML_open_folder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_generate_XML_Click(object sender, EventArgs e)
        {
            string rootPath = combobox_rootpath.Text;
            var dir = new DirectoryInfo(rootPath);

            var doc = new XDocument(GetDirectoryXml(dir));

            doc.Save("test.xml");
        }

        private void button_open_folder_Click(object sender, EventArgs e)
        {

        }

        private void textbox_folder_to_find_TextChanged(object sender, EventArgs e)
        {

        }

        private void combobox_rootpath_SelectedIndexChanged(object sender, EventArgs e)
        {
            combobox_rootpath.Items.Clear();
            foreach (string s in Directory.GetLogicalDrives())
            {

                combobox_rootpath.Items.Add(s);


            }
        }
        public static XElement GetDirectoryXml(DirectoryInfo dir)
        {
            var info = new XElement("dir",
                           new XAttribute("name", dir.Name));

            //foreach (var file in dir.GetFiles())
            // info.Add(new XElement("file",
            //  new XAttribute("name", file.Name)));

            foreach (var subDir in dir.GetDirectories())
                info.Add(GetDirectoryXml(subDir));

            return info;
        }

    }
}
以下是XML文件内容的示例:



是。但是stackoverflow不是一个代码编写服务。你得自己试试。当你有具体的问题时,回来问他们。我不想问代码!!!!只需要一些灯!!!!例如,我将文件夹树转换为XML文档。。。。