how to read all files inside particular folder
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
.
using System.IO;
DirectoryInfo di = new DirectoryInfo(folder);
FileInfo[] files = di.GetFiles("*.xml");
using System.IO;
//...
string[] files;
if (Directory.Exists(Path)) {
files = Directory.GetFiles(Path, @"*.xml", SearchOption.TopDirectoryOnly);
//...
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());
}
}
}
}