0

我正在使用 vb.net 为 Windows 应用程序编写代码。我想在c:\. 如果该文件已经存在,我想删除该文件。

my code
-------
 Dim file As String = "C:\test.txt"
    If System.IO.File.Exists(file) Then
        file.Remove(file)
    Else

        System.Diagnostics.Process.Start(file)

  End If 

当我尝试打开该文件时出现以下错误。

error
-----
The system cannot find the file specified  
4

1 回答 1

2

除了 Konrad 关于尝试执行您刚刚检查的文件不存在的观点之外:

1) 命名变量不是一个好主意,file因为它可能会与 System.IO.File 混淆。

2) 它是 File.Delete,而不是 file.Remove - 您正在调用 String.Remove 方法,因为file它是一个字符串。您应该使用Option Strict On,因为它会为您捕获该错误。

3) 在 Windows Vista 和更高版本上,您可能没有对 C: 的写入/删除权限。

假设您对目录 C:\temp 具有写访问权限,则此方法有效:

Dim fyle As String = "C:\temp\test.txt"

If System.IO.File.Exists(fyle) Then
    IO.File.Delete(fyle)
End If

IO.File.Create(fyle)
System.Diagnostics.Process.Start(fyle)
于 2012-07-16T20:21:33.447 回答