for循环不能使用Count()

-5 c# for-loop

我创建了一个程序,用于复制zip存档中的目录和一些文件.

因此,当我启动程序时,没有存档.我试图放置一个断点,它显示for循环正在跳过.为什么?

for (int i = 0; i == steamPath.Count(s => s != null); i++)
{
    string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    ZipFile zip = new ZipFile();
    zip.AddFile(Path.Combine(path[i], "folder"));
    foreach (string f in Directory.GetFiles(path[i]))
    {
        string fileName = Path.GetFileName(f);
        if(fileName.StartsWith("name"))
        {
            zip.AddFile(Path.Combine(path[i], fileName));
        }
    }
    zip.Save(Path.Combine(appData, "File[" + i + "].zip"));
}
Run Code Online (Sandbox Code Playgroud)

Pat*_*man 6

因为如果计数超过...... i == steamPath.Count(s => s != null)将永远不会导致...因此,循环将会中断.true0

试试这个:

i < steamPath.Count(s => s != null)
Run Code Online (Sandbox Code Playgroud)

for循环运行,只要条件是true,不,直到true.

另一种选择是使用foreach:

foreach (var path in steamPath.Where(s => s != null)))
{ }
Run Code Online (Sandbox Code Playgroud)