为什么我的 if 语句没有停止?

发布于 2024-11-09 11:22:21 字数 1366 浏览 0 评论 0原文

我对 C# 还是个新手,一直在解决这个问题。

我正在尝试读取文件并获取文件扩展名,如果文件名末尾没有“.done”则继续。然后在代码的其余部分中,我将数据上传到数据库并将文件名更改为“.done”。

问题是我查看文件的 If 语句返回 true 并再次上传所有文件,而不仅仅是需要上传的文件。我尝试了很多方法,但似乎都不起作用。

DirectoryInfo DirInfo = new DirectoryInfo(sFolder);

FileInfo[] fileEntries = DirInfo.GetFiles("*.*", System.IO.SearchOption.TopDirectoryOnly);

            foreach (FileInfo fileInfo in fileEntries)
            {
                if (fileInfo.Extension != ".done")
                    continue;

                // More code
             }

我也通过获取所有文件尝试过此操作。它仍然循环并再次上传所有文件,即使是那些带有“.done”的文件。

DirectoryInfo DirInfo = new DirectoryInfo(sFolder);

FileInfo[] fileEntries = DirInfo.GetFiles();

            foreach (FileInfo fileInfo in fileEntries)
            {
                if (fileInfo.Name != ".done")
                    continue;

                // More code
             }

该代码有效,但我需要更改它,因为文件扩展名发生了变化。

DirectoryInfo DirInfo = new DirectoryInfo(sFolder);

FileInfo[] fileEntries = DirInfo.GetFiles("*.ivc", System.IO.SearchOption.TopDirectoryOnly);

            foreach (FileInfo fileInfo in fileEntries)
            {
                if (fileInfo.Name == ".ivc")
                    continue;

                // More code
             }

任何事情都会有所帮助。

谢谢

I am still new to C# and stuck on this problem.

I am trying to read a file and get the file extension and if the file name does not have '.done' at the end to continue. Then in the rest of my code I upload the data to the database and change the file name to '.done'.

Problem is that my If statement that looks at the file is returning true and uploading all the files again instead of just the ones that need to be uploaded. I have tried many ways and none seem to work.

DirectoryInfo DirInfo = new DirectoryInfo(sFolder);

FileInfo[] fileEntries = DirInfo.GetFiles("*.*", System.IO.SearchOption.TopDirectoryOnly);

            foreach (FileInfo fileInfo in fileEntries)
            {
                if (fileInfo.Extension != ".done")
                    continue;

                // More code
             }

I have tried this also by get all the files. It still loops and uploads all the files again even the ones that have '.done'.

DirectoryInfo DirInfo = new DirectoryInfo(sFolder);

FileInfo[] fileEntries = DirInfo.GetFiles();

            foreach (FileInfo fileInfo in fileEntries)
            {
                if (fileInfo.Name != ".done")
                    continue;

                // More code
             }

This code works, but I need to change it because file extensions change.

DirectoryInfo DirInfo = new DirectoryInfo(sFolder);

FileInfo[] fileEntries = DirInfo.GetFiles("*.ivc", System.IO.SearchOption.TopDirectoryOnly);

            foreach (FileInfo fileInfo in fileEntries)
            {
                if (fileInfo.Name == ".ivc")
                    continue;

                // More code
             }

Anything will be of help.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧伤还要旧人安 2024-11-16 11:22:21

你的布尔条件条件没有倒退吗?如果扩展名不是 .done,您希望执行其余代码,如果扩展名是 .done,则转到循环的下一次迭代,对吗?当扩展名不是 .done 时, continue 关键字将带您进入下一次迭代!

foreach (FileInfo fileInfo in fileEntries)
{
    if (fileInfo.Name == ".done")
        continue;

    // More code
}

Don't you have your boolean conditional backwards? You want to to do the rest of your code if the extension IS NOT .done, and go to the next iteration of the loop if IT IS .done, right? The continue keyword is taking you to the next iteration when the extension is not .done!

foreach (FileInfo fileInfo in fileEntries)
{
    if (fileInfo.Name == ".done")
        continue;

    // More code
}
转瞬即逝 2024-11-16 11:22:21

为什么不只要求具有您要查找的扩展名的文件呢?

DirectoryInfo DirInfo = new DirectoryInfo(sFolder);

FileInfo[] fileEntries = DirInfo.GetFiles("*.done", System.IO.SearchOption.TopDirectoryOnly);

foreach (FileInfo fileInfo in fileEntries)
{
    // do stuff
}

Why not ask for only the files that have the extension you're looking for?

DirectoryInfo DirInfo = new DirectoryInfo(sFolder);

FileInfo[] fileEntries = DirInfo.GetFiles("*.done", System.IO.SearchOption.TopDirectoryOnly);

foreach (FileInfo fileInfo in fileEntries)
{
    // do stuff
}
柠北森屋 2024-11-16 11:22:21

我建议尽可能使用积极思考的“最佳编码”实践(这个建议也可以应用于生活),因为它们通常更容易阅读。不要使用“不等于”条件,而是使用“等于”条件,如下所示:

        // just for this example
        var folder = Environment.CurrentDirectory;

        var dirInfo = new DirectoryInfo(folder);

        // type of files to retrieve
        // @ is for 'string literal' (see link below)
        var extension = @".ivc";  
        // this translates to: "*.ivc"
        var allFiles = @"*" + extension;

        // the extension of files already processed, and hence, skipped
        var skipExtension = @".done";

        var fileEntries = dirInfo.GetFiles(allFiles, System.IO.SearchOption.TopDirectoryOnly);

        // track number of files processed
        var filesProcessed = 0;

        foreach (var fileInfo in fileEntries)
        {
            // skip files that have already been processed
            if (fileInfo.Extension == skipExtension)
            {
                continue;
            }

            // process new files found in this directory
            filesProcessed++;
        }

参考文献:
字符串文字

I suggest using the 'best coding' practice of using positive thinking when possible (this advice can also be applied to life), because they are generally easier to read. Instead of using a "not equal" condition, use an "is equal" condition, like so:

        // just for this example
        var folder = Environment.CurrentDirectory;

        var dirInfo = new DirectoryInfo(folder);

        // type of files to retrieve
        // @ is for 'string literal' (see link below)
        var extension = @".ivc";  
        // this translates to: "*.ivc"
        var allFiles = @"*" + extension;

        // the extension of files already processed, and hence, skipped
        var skipExtension = @".done";

        var fileEntries = dirInfo.GetFiles(allFiles, System.IO.SearchOption.TopDirectoryOnly);

        // track number of files processed
        var filesProcessed = 0;

        foreach (var fileInfo in fileEntries)
        {
            // skip files that have already been processed
            if (fileInfo.Extension == skipExtension)
            {
                continue;
            }

            // process new files found in this directory
            filesProcessed++;
        }

References:
String literals

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文