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")));
You can use the DirectoryInfo.GetFiles
method:
FileInfo[] files = DirectoryInfo.GetFiles("*.xml");
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());
}
}
}
}
Try this It is working for me..
The syntax is 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.
using System.IO;
string[] arr=Directory.GetFiles("folderpath","*.Fileextension");
foreach(string file in arr)
{
}
using System.IO;
//...
string[] files;
if (Directory.Exists(Path)) {
files = Directory.GetFiles(Path, @"*.xml", SearchOption.TopDirectoryOnly);
//...
using System.IO;
DirectoryInfo di = new DirectoryInfo(folder);
FileInfo[] files = di.GetFiles("*.xml");
Source: Stackoverflow.com