试图将一些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);'因为你不需要初始化迭代变量。 –