无法删除或启动文件/程序

时间:2015-04-20 12:59:25

标签: vb.net

我无法在visual basic中启动或删除文件 并且每次运行时都会说出分散文件:

"C:\random\test.exe"

它弹出一个错误,它说:无法将String test.exe转换为boolean格式。 (来自瑞典语的粗略翻译),下面是代码:

ElseIf t = "run" And Dir(arg1.Text) Then
    System.Diagnostics.Process.Start(arg1.Text)
ElseIf t = "del" And Dir(arg1.Text) Then
    My.Computer.FileSystem.DeleteFile(arg1.Text)
End If

它可能很容易看到,但我很愚蠢,无法找到它; c

2 个答案:

答案 0 :(得分:2)

函数FileSystem.Dir()返回一个字符串而不是布尔值,因此VB.NET无法检查您的条件(因此错误)。

如果您的目标是检查文件是否存在,请在File.exists()命名空间中使用System.IO

if (File.Exists(arg1.Text))
   'Do a lot of stuff.

答案 1 :(得分:1)

您正尝试向If语句提供表示目录的字符串:

If t = "run" And Dir(arg1.Text) Then
    ...
End If

可以翻译为:

If (the text is "run") while ("C:\random\test.exe") Then
    ???
End If

当然,您很难理解并且编译器无法解释。 您可能想要检查目录是否存在,您可以这样做:

If t = "run" And File.Exists(arg1.Text) Then
    ...
End If
相关问题