2016-03-14 27 views
0

我有一个鳕鱼与此鳕鱼我可以得到我的应用程序文件夹中的文件,但我想要另一个文件夹文件了。如何获得C#中的另一个文件夹文件并显示在XML

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     string[] files; 
     private void button1_Click(object sender, EventArgs e) 
     { 

      string appPath = Path.GetDirectoryName(Application.ExecutablePath); 

      string folder = appPath;//Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Archive\"; 
      string filter = "*.*"; 
      files = Directory.GetFiles(folder, filter); 

      var doc = new XmlDocument(); 
      var root = doc.AppendChild(doc.CreateElement("Item")); 

      foreach (var item in files) 
      { 
       var name = root.AppendChild(doc.CreateElement("Name")); 
       string string1 = item; 
       string string2 = appPath; 
       string result = string1.Replace(string2, ""); 
       name.InnerText = result; 
       MessageBox.Show(result); 
      } 

      using (var writer = new XmlTextWriter("data.xml", null)) 
      { 
       writer.Formatting = Formatting.Indented; 
       doc.Save(writer); 
      } 
      { 

      } 
     } 

XML输出

<?xml version="1.0"?> 
<Item> 
    <Name>\data.xml</Name> 
    <Name>\dataa.xml</Name> 
    <Name>\WindowsFormsApplication9.exe</Name> 
    <Name>\WindowsFormsApplication9.exe.config</Name> 
    <Name>\WindowsFormsApplication9.pdb</Name> 
    <Name>\WindowsFormsApplication9.vshost.exe</Name> 
    <Name>\WindowsFormsApplication9.vshost.exe.config</Name> 
    <Name>\WindowsFormsApplication9.vshost.exe.manifest</Name> 
</Item> 

我有3个文件夹在我的app文件夹过的任何文件夹我想要的任何文件夹中的我的XML编写。

+1

你说的其他文件夹中的文件是什么意思?在您的本地系统文件夹? – VVN

回答

0

如果你想也列出子文件夹中的文件,而不是

files = Directory.GetFiles(folder, filter); 

可以使用

files = Directory.EnumerateFiles(folder, filter, SearchOption.AllDirectories).ToArray(); 
相关问题