开发者

Why is my if statement not stopping?

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")
                    co开发者_开发问答ntinue;

                // 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


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
}


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
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜