當我運行時,我只能得到Month.txt
文件的內容,我明白這只是因爲我已經將它設置爲foreach
中的字符串,但是我無法弄清楚如何添加其他文件到這個以及我得到的所有文件的內容,而不僅僅是本月?foreach讀取多個數組
string[] month = System.IO.File.ReadAllLines
(@"E:\project1\input\Month.txt");
string[] 1_AF = System.IO.File.ReadAllLines
(@"E:\project1\input\1_AF.txt");
string[] 1_Rain = System.IO.File.ReadAllLines
(@"E:\project1\input\1_Rain.txt");
string[] 1_Sun = System.IO.File.ReadAllLines
(@"E:\project1\input\1_Sun.txt");
string[] 1_TBig = System.IO.File.ReadAllLines
(@"E:\project1\input\1_TBig.txt");
string[] 1_TSmall = System.IO.File.ReadAllLines
(@"E:\project1\input\1_TSmall.txt");
System.Console.WriteLine("Contents of all files =:");
foreach (string months in month)
{
Console.WriteLine(months + "\t" + 1_AF + "\t" + 1_Rain + "\t" + 1_Sun + "\t" + 1_TBig + "\t" + 1_TSmall);
}
Console.ReadKey();
你必須創建數組列表並添加單個文件的內容放到這個名單,那麼你可以申請的foreach爲數組 –
你有沒有考慮使用'List','Dictionary',或'Tuple'?它們對於像您的情況非常有用。 – Ian