繁体   English   中英

使用vb.net打开文本文件,如果存在则删除该文件

[英]Open a text file with vb.net , and if it exists delete that file

我正在使用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  

除了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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM