更新时间:2023-01-23 10:11:34
使用System.IO.StreamReader / StreamWriter读取/写入文本文件。
例如...使用 System.IO;
DirectoryInfo di = new DirectoryInfo( <包含目录的路径>);
FileInfo [] files = di.GetFiles();
string data = 跨度>;
foreach (FileInfo fi in files)
{
if (fi.Name.ToUpper()。StartsWith( A_L_PRTD))
{
StreamReader rd = new StreamReader(fi.FullName);
data + = rd.ReadToEnd();
rd.Close();
rd.Dispose();
// 可选...
File.Delete(fi.FullName) ;
}
}
StreamWriter wr = new StreamWriter( string .Format( L_PRTD_ {0} .txt,DateTime.Now.ToString( ddMMyyyy)));
wr.Write(data);
wr.Close();
wr.Dispose();
试试这样..string path = d://文件//跨度>;
// string [] fileNames = {A_L_PRTD_432124.txt,A_L_PRTD_021345.txt};
string fileNameStartsWIth = A_L_PRTD_ 跨度>;
string [] fileNames = Directory
.GetFiles(path, * .txt,SearchOption.AllDirectories)
.Select(f = > Path.GetFileName( f))。其中(k = > k.StartsWith(fileNameStartsWIth))。ToArray();
string finalFile = L_PRTD _ + DateTime.Now.ToString( ddMMMyyyy)+ 。txt;
string contentSeperator = Environment.NewLine + -------------------------------------------------- --- + Environment.NewLine;
foreach (字符串文件 in fileNames)
{
string content = File.ReadAllText(path + file)+ contentSeperator;
File.AppendAllText(path + finalFile,content);
}
参考:
文件.ReadAllText [ ^ ]
File.AppendText [ ^ ]
试试这个File.AppendAllText(路径,内容);
Hello I have multiple text files in one location and they are like the name below :
A_L_PRTD_021345.txt , A_L_PRTD_432124 , like this ..
I want to merge them into single file and want to Rename the single file as L_PRTD_Currentdate>.txt
How can It be possible ? Kindly share me some cod of doing this
Use System.IO.StreamReader/StreamWriter to read/write the text files.
For example...using System.IO; DirectoryInfo di = new DirectoryInfo("<path to containing directory>"); FileInfo[] files = di.GetFiles(); string data = ""; foreach (FileInfo fi in files) { if (fi.Name.ToUpper().StartsWith("A_L_PRTD")) { StreamReader rd = new StreamReader(fi.FullName); data += rd.ReadToEnd(); rd.Close(); rd.Dispose(); //Optionally... File.Delete(fi.FullName); } } StreamWriter wr = new StreamWriter(string.Format("L_PRTD_{0}.txt", DateTime.Now.ToString("ddMMyyyy"))); wr.Write(data); wr.Close(); wr.Dispose();
try like this..string path = "D://Files//"; //string[] fileNames = { "A_L_PRTD_432124.txt", "A_L_PRTD_021345.txt" }; string fileNameStartsWIth = "A_L_PRTD_"; string[] fileNames = Directory .GetFiles(path, "*.txt", SearchOption.AllDirectories) .Select(f => Path.GetFileName(f)).Where(k => k.StartsWith(fileNameStartsWIth)).ToArray(); string finalFile = "L_PRTD_" + DateTime.Now.ToString("ddMMMyyyy") + ".txt"; string contentSeperator = Environment.NewLine + "-----------------------------------------------------" + Environment.NewLine; foreach (string file in fileNames) { string content = File.ReadAllText(path + file) + contentSeperator; File.AppendAllText(path + finalFile, content); }
reference:
File.ReadAllText [^]
File.AppendText[^]
try thisFile.AppendAllText(Path, content);