Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 带或和的IF语句_Vb.net_Visual Studio 2010_If Statement - Fatal编程技术网

Vb.net 带或和的IF语句

Vb.net 带或和的IF语句,vb.net,visual-studio-2010,if-statement,Vb.net,Visual Studio 2010,If Statement,下面是我当前的if声明。我正在尝试检查某些文件是否存在,但我的第一次检查(使用或/和)似乎没有拾取任何这些文件,因此无法转到msgbox状态。我已经添加了我认为有意义的地方,但仍然没有找到任何文件 目前,文件2.txt和4.txt确实存在于C:\Temp中 对我遗漏的东西有什么建议吗 If (My.Computer.FileSystem.FileExists("C:\Temp\1.txt") Or _ My.Computer.FileSystem.FileExists("C:\Temp\

下面是我当前的if声明。我正在尝试检查某些文件是否存在,但我的第一次检查(使用或/和)似乎没有拾取任何这些文件,因此无法转到msgbox状态。我已经添加了我认为有意义的地方,但仍然没有找到任何文件

目前,文件2.txt和4.txt确实存在于C:\Temp中

对我遗漏的东西有什么建议吗

If (My.Computer.FileSystem.FileExists("C:\Temp\1.txt") Or _
    My.Computer.FileSystem.FileExists("C:\Temp\2.txt") Or _
    My.Computer.FileSystem.FileExists("C:\Temp\3.txt")) And _
    My.Computer.FileSystem.FileExists("C:\Temp\4.txt") Then

<<do something here1>>

Else

Msgbox("No files exist.")

EndIf

如果要检查是否存在所有文件,请使用AndAlso: 如果四个文件都存在,则返回true

If  My.Computer.FileSystem.FileExists("C:\Temp\1.txt") AndAlso
    My.Computer.FileSystem.FileExists("C:\Temp\2.txt") AndAlso
    My.Computer.FileSystem.FileExists("C:\Temp\3.txt") AndAlso
    My.Computer.FileSystem.FileExists("C:\Temp\4.txt") Then

    <<do something here1>>

Else

   Msgbox("No files exist.")

EndIf

最终,我错过了一次机会

感谢@Dan Puzey建议使用布尔值检查文件是否存在。在使用该文件时,我的文件确实不存在,因为该文件有额外的空间

最后我仍然使用IF语句,添加了两个额外的paren,现在它似乎起作用了

@varocarbas最初建议将此作为一个答案,但仍然不太清楚为什么会将其删除,但他希望在应该得到信任的地方给予信任

If ((My.Computer.FileSystem.FileExists("C:\Temp\1.txt") Or _
    My.Computer.FileSystem.FileExists("C:\Temp\2.txt") Or _
    My.Computer.FileSystem.FileExists("C:\Temp\3.txt"))) And _
    My.Computer.FileSystem.FileExists("C:\Temp\4.txt") Then

<<do something here1>>

Else

Msgbox("No files exist.")

EndIf

If条件是否总是计算为False?另外,你有没有试过在你的手表窗口中单独放置每一条语句来挑出违规的情况?你说它不符合这张支票。你这是什么意思?你是说代码根本没有运行,还是说它总是在你认为应该成功的测试中失败?假设是后者,建议您帮助调试:将每次调用.FileExists的结果分配给本地布尔变量Exists1、Exists2等,并在If语句中使用这些布尔值。通过这种方式,您可以更轻松地找出为什么您的条件与您期望的条件有不同的值。@DanPuzey更正,它的故障在于msgbox,它实际上在目录2.txt和4.txt中有文件。这不是注释部分,也不是聊天和开玩笑的地方。最好删除所有主题外的内容。@varocarbas:发布的代码没有不可理解的地方。评论帖子中的爆炸完全是因为你误读了这个问题,然后当人们认为你的解决方案没有用时,你就采取了激进的行动。让它过去,继续。使用OrElse和AndAlso不会对表达式的结果产生任何影响-这并没有回答最初的问题。
If ((My.Computer.FileSystem.FileExists("C:\Temp\1.txt") Or _
    My.Computer.FileSystem.FileExists("C:\Temp\2.txt") Or _
    My.Computer.FileSystem.FileExists("C:\Temp\3.txt"))) And _
    My.Computer.FileSystem.FileExists("C:\Temp\4.txt") Then

<<do something here1>>

Else

Msgbox("No files exist.")

EndIf