ram
ram

Reputation: 1319

how to read all files inside particular folder

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

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.

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

Upvotes: 130

Views: 347153

Answers (7)

Chandan Kumar
Chandan Kumar

Reputation: 221

Try this It is working for me..

The syntax is Directory.GetFiles(string path, string searchPattern);

var filePath = Server.MapPath("~/App_Data/");
string[] filePaths = Directory.GetFiles(@filePath, "*.*");

This code will return all the files inside App_Data folder.

The second parameter . indicates the searchPattern with File Extension where the first * is for file name and second is for format of the file or File Extension like (*.png - any file name with .png format.

Upvotes: 6

Rahul sahu
Rahul sahu

Reputation: 31

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

       }

Upvotes: 2

Adi
Adi

Reputation: 5223

using System.IO;

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

Upvotes: 36

gaurav balyan
gaurav balyan

Reputation: 61

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());
      }   
    }
  }
}

Upvotes: 6

trydis
trydis

Reputation: 3925

You can use the DirectoryInfo.GetFiles method:

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

Upvotes: 8

Marc Gravell
Marc Gravell

Reputation: 1064114

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.

Upvotes: 311

Matthias Alleweldt
Matthias Alleweldt

Reputation: 2453

using System.IO;

//...

  string[] files;

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

Upvotes: 16

Related Questions