English 中文(简体)
• 如何通过一个论坛获取档案信息?
原标题:How to get FileInfo via a foreach Loop?

Trying to convert some VB to C#... (learning C#, too). I have some code that loops through files in a directory and retrieves their file information. I have this originally in VB, but am trying to learn C#, and the online convertors don t give me code that will pass .net 2.0.

Here is the error: Type and identifier are both required in a foreach statement

这里,我有:

DirectoryInfo dirInfo = new DirectoryInfo(currentDir);
FileInfo[] files = null;
files = dirInfo.GetFiles();

FileInfo f = default(FileInfo);
foreach (f in files) {  ...
}

I tried putting foreach(FileInfo f... but it gives me a different error: 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

我如何确定这一点?

最佳回答
DirectoryInfo dirInfo = new DirectoryInfo(currentDir);
FileInfo[] files = null;
files = dirInfo.GetFiles();

// I removed the declaration of f here to prevent the name collision.
foreach (FileInfo f in files)
{  ...
}

这里是一部更简单的法典:

DirectoryInfo dirInfo = new DirectoryInfo(currentDir);
foreach (FileInfo f in dirInfo.GetFiles())
{
}
问题回答

这里,它希望你再犯:

FileInfo f = default(FileInfo);
foreach (f in files) {  ...
}

你们正在 lo子外面界定 f,然后试图在 lo体内加以界定。

如果你需要缺省,则尝试:

FileInfo f = default(FileInfo);
foreach (FileInfo file in files)
    {
         relevant code here
    }

否则删除宣布变数“f”的声明

您应提供体内使用的变量类型。 您将采用<代码>FileInfo。 但是,如果是C# 3.0或之后,你只能写上var<>/code>,汇编者将照发:

foreach (FileInfo f in files) 
{  
   // ...
}

http://msdn.microsoft.com/en-us/library/a664754%28v=vs.71%29.aspx”rel=“nofollow”>。

完整的解决办法(现在不需要先入代变数和档案系列):

DirectoryInfo dir = new DirectoryInfo(currentDir);
foreach (FileInfo file in dir.GetFiles()) 
{
   // use file  
}

这一工作应当:

        DirectoryInfo dirInfo = new DirectoryInfo(currentDir);
        FileInfo[] files = null;
        files = dirInfo.GetFiles();
        foreach (FileInfo f in files)
        {
        }

Edit:

我认为,这将更清洁:

        foreach (FileInfo f in new DirectoryInfo(currentDir).GetFiles())
            {
            }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...