嗯,那個不理它了。我寫好測試過後再給你。
授人以渔,不授人以鱼。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using namespace wube_01 { class Program { static internal Int32 TitleLines; static internal List<StringBuilder> TargetData = new List<StringBuilder>(); static internal void Main(String[] args) { String programFileName = Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]); if ((args.Length < 3) || !Int32.TryParse(args[2], out TitleLines)) { ShowTips(programFileName); return; } String targetFileName = args[0]; String sourceDataPath = args[1]; String[] files = Directory.GetFiles(sourceDataPath); for (Int32 index = 0; index < files.Length; ++index) { Console.SetCursorPosition(0, 0); Console.WriteLine("正在處理{0},請稍候...", Path.GetFileName(files[index])); Merger(targetFileName, files[index]); } WriteOutputFile(targetFileName); Console.Write("按回車鍵結束程序..."); Console.ReadLine(); } // 顯示命令行語法幫助 static internal void ShowTips(String programFileName) { Console.WriteLine("格式: {0} 輸出文檔名 \"數據源文檔所在路徑\" 標題行數", programFileName); Console.WriteLine("例: {0} AAAA.csv \"Data\" 6", programFileName); Console.WriteLine(" 數據源文檔放在當前目錄的子目錄Data中,標題佔用6行"); } // 合併兩個文檔 static internal void Merger(String targetFileName, String sourceFileName) { StreamReader sourceReader = (new FileInfo(sourceFileName)).OpenText(); Int32 index = 0; while (!sourceReader.EndOfStream) { String sourceLine = sourceReader.ReadLine(); if (index >= TargetData.Count) { TargetData.Add(new StringBuilder(sourceLine)); } else { TargetData[index].Append(sourceLine); } ++index; } sourceReader.Close(); } // 寫最終輸出文檔 static internal void WriteOutputFile(String fileName) { StreamWriter targetWriter = (new FileInfo(fileName)).CreateText(); foreach (StringBuilder line in TargetData) { targetWriter.WriteLine(line); } targetWriter.Close(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using namespace wube_01 { class Program { static internal Int32 TitleLines; static internal List<StringBuilder> TargetData = new List<StringBuilder>(); static internal void Main(String[] args) { String programFileName = Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]); if ((args.Length < 3) || !Int32.TryParse(args[2], out TitleLines)) { ShowTips(programFileName); return; } String targetFileName = args[0]; String sourceDataPath = args[1]; String[] files = Directory.GetFiles(sourceDataPath); for (Int32 index = 0; index < files.Length; ++index) { Console.SetCursorPosition(0, 0); Console.WriteLine("正在處理{0},請稍候...", Path.GetFileName(files[index])); Merger(files[index]); } WriteOutputFile(targetFileName); Console.Write("按回車鍵結束程序..."); Console.ReadLine(); } // 顯示命令行語法幫助 static internal void ShowTips(String programFileName) { Console.WriteLine("格式: {0} 輸出文檔名 \"數據源文檔所在路徑\" 標題行數", programFileName); Console.WriteLine("例: {0} AAAA.csv \"Data\" 6", programFileName); Console.WriteLine(" 數據源文檔放在當前目錄的子目錄Data中,標題佔用6行"); } // 合併兩個文檔 static internal void Merger(String sourceFileName) { StreamReader sourceReader = (new FileInfo(sourceFileName)).OpenText(); Int32 index = 0; while (!sourceReader.EndOfStream) { String sourceLine = sourceReader.ReadLine(); if (index >= TargetData.Count) { TargetData.Add(new StringBuilder(sourceLine)); } else { TargetData[index].Append(sourceLine); } ++index; } sourceReader.Close(); } // 寫最終輸出文檔 static internal void WriteOutputFile(String fileName) { StreamWriter targetWriter = (new FileInfo(fileName)).CreateText(); foreach (StringBuilder line in TargetData) { targetWriter.WriteLine(line); } targetWriter.Close(); } } }