試圖將一些VB轉換爲C#...(也學習C#)。 我有一些代碼循環通過目錄中的文件並檢索其文件信息。我原本在VB中,但我試圖學習C#,並且在線轉換器不會給我通過.net 2.0的代碼。如何通過foreach循環獲取FileInfo?
以下是錯誤: Type and identifier are both required in a foreach statement
這裏是我的代碼:我試圖把foreach(FileInfo f...
DirectoryInfo dirInfo = new DirectoryInfo(currentDir);
FileInfo[] files = null;
files = dirInfo.GetFiles();
FileInfo f = default(FileInfo);
foreach (f in files) { ...
}
,但它給了我一個不同的錯誤: A local variable named 'f' cannot be declared in this scope because it would give a different meaning to 'f', which is already used in a 'parent or current' scope to denote something else
我如何修理它?
'的foreach語句;你會得到什麼錯誤? – carlosfigueira
在問題中增加了錯誤。抱歉。我認爲這也應該工作,但這是.net2,而不是.net4 - 也許這有所作爲? – bgmCoder
只需刪除'FileInfo f = default(FileInfo);'因爲你不需要初始化迭代變量。 –