如何读取特定文件夹中的所有文件

时间:2023-01-14 11:35:44

I want to read all xml files inside a particular folder in c# .net

我想要读取c# .net中特定文件夹中的所有xml文件

XDocument doc2 = XDocument.Load((PG.SMNR.XMLDataSourceUtil.GetXMLFilePath(Locale, "Products/category/product.xml")));

i have multiple products in category folder.. want loop the folder and should get all product xml file names.

我在category文件夹有多个产品。要循环文件夹,并应该获得所有产品xml文件名。

XDocument doc2 = XDocument.Load((PG.SMNR.XMLDataSourceUtil.GetXMLFilePath(Locale, "Products/category/x1.xml")));

6 个解决方案

#1


178  

using System.IO;
...
foreach (string file in Directory.EnumerateFiles(folderPath, "*.xml"))
{
    string contents = File.ReadAllText(file);
}

Note the above uses a .NET 4.0 feature; in previous versions replace EnumerateFiles with GetFiles). Also, replace File.ReadAllText with your preferred way of reading xml files - perhaps XDocument, XmlDocument or an XmlReader.

注意,上面使用了。net 4.0特性;在以前的版本中,使用GetFiles替换EnumerateFiles)。此外,替换文件。用您喜欢的方式读取xml文件的ReadAllText——可能是XDocument、XmlDocument或XmlReader。

#2


17  

using System.IO;

DirectoryInfo di = new DirectoryInfo(folder);
FileInfo[] files = di.GetFiles("*.xml");

#3


12  

using System.IO;

//...

  string[] files;

  if (Directory.Exists(Path)) {
    files = Directory.GetFiles(Path, @"*.xml", SearchOption.TopDirectoryOnly);
    //...

#4


5  

You can use the DirectoryInfo.GetFiles method:

你可以使用DirectoryInfo。getfile方法:

FileInfo[] files = DirectoryInfo.GetFiles("*.xml");

#5


5  

If you are looking to copy all the text files in one folder to merge and copy to another folder, you can do this to achieve that:

如果你想把所有的文本文件复制到一个文件夹中合并到另一个文件夹中,你可以这样做:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HowToCopyTextFiles
{
  class Program
  {
    static void Main(string[] args)
    {
      string mydocpath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);     
      StringBuilder sb = new StringBuilder();
      foreach (string txtName in Directory.GetFiles(@"D:\Links","*.txt"))
      {
        using (StreamReader sr = new StreamReader(txtName))
        {
          sb.AppendLine(txtName.ToString());
          sb.AppendLine("= = = = = =");
          sb.Append(sr.ReadToEnd());
          sb.AppendLine();
          sb.AppendLine();   
        }
      }
      using (StreamWriter outfile=new StreamWriter(mydocpath + @"\AllTxtFiles.txt"))
      {    
        outfile.Write(sb.ToString());
      }   
    }
  }
}

#6


-1  

    using System.IO;
    string[] arr=Directory.GetFiles("folderpath","*.Fileextension");
      foreach(string file in arr)
       {

       }

#1


178  

using System.IO;
...
foreach (string file in Directory.EnumerateFiles(folderPath, "*.xml"))
{
    string contents = File.ReadAllText(file);
}

Note the above uses a .NET 4.0 feature; in previous versions replace EnumerateFiles with GetFiles). Also, replace File.ReadAllText with your preferred way of reading xml files - perhaps XDocument, XmlDocument or an XmlReader.

注意,上面使用了。net 4.0特性;在以前的版本中,使用GetFiles替换EnumerateFiles)。此外,替换文件。用您喜欢的方式读取xml文件的ReadAllText——可能是XDocument、XmlDocument或XmlReader。

#2


17  

using System.IO;

DirectoryInfo di = new DirectoryInfo(folder);
FileInfo[] files = di.GetFiles("*.xml");

#3


12  

using System.IO;

//...

  string[] files;

  if (Directory.Exists(Path)) {
    files = Directory.GetFiles(Path, @"*.xml", SearchOption.TopDirectoryOnly);
    //...

#4


5  

You can use the DirectoryInfo.GetFiles method:

你可以使用DirectoryInfo。getfile方法:

FileInfo[] files = DirectoryInfo.GetFiles("*.xml");

#5


5  

If you are looking to copy all the text files in one folder to merge and copy to another folder, you can do this to achieve that:

如果你想把所有的文本文件复制到一个文件夹中合并到另一个文件夹中,你可以这样做:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HowToCopyTextFiles
{
  class Program
  {
    static void Main(string[] args)
    {
      string mydocpath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);     
      StringBuilder sb = new StringBuilder();
      foreach (string txtName in Directory.GetFiles(@"D:\Links","*.txt"))
      {
        using (StreamReader sr = new StreamReader(txtName))
        {
          sb.AppendLine(txtName.ToString());
          sb.AppendLine("= = = = = =");
          sb.Append(sr.ReadToEnd());
          sb.AppendLine();
          sb.AppendLine();   
        }
      }
      using (StreamWriter outfile=new StreamWriter(mydocpath + @"\AllTxtFiles.txt"))
      {    
        outfile.Write(sb.ToString());
      }   
    }
  }
}

#6


-1  

    using System.IO;
    string[] arr=Directory.GetFiles("folderpath","*.Fileextension");
      foreach(string file in arr)
       {

       }