为什么我的 if 语句没有停止?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的布尔条件条件没有倒退吗?如果扩展名不是 .done,您希望执行其余代码,如果扩展名是 .done,则转到循环的下一次迭代,对吗?当扩展名不是 .done 时, continue 关键字将带您进入下一次迭代!
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!
为什么不只要求具有您要查找的扩展名的文件呢?
Why not ask for only the files that have the extension you're looking for?
我建议尽可能使用积极思考的“最佳编码”实践(这个建议也可以应用于生活),因为它们通常更容易阅读。不要使用“不等于”条件,而是使用“等于”条件,如下所示:
参考文献:
字符串文字
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:
References:
String literals