【问题标题】:Visual Basic: Checking if file exists, if not, create the fileVisual Basic:检查文件是否存在,如果不存在,则创建文件
【发布时间】:2014-04-08 12:02:17
【问题描述】:

这是代码。它检查路径中的文件是否存在,如果不存在,则创建该文件。我一直收到此错误消息,我不知道为什么。也许我应该关闭 System.IO.Directory.Exists?如果是,我该怎么做?请注意,我正在创建一个文本文件。

代码

If Not (System.IO.Directory.Exists(path)) Then
        Dim fs3 As FileStream = File.Create(path)
    End If

这是我收到的错误消息:

进程无法使用该文件(路径),因为其他进程目前正在使用该文件。

【问题讨论】:

  • 我怀疑问题背后的原因与IO Exception was unhandled error 中的相同。否则,您是否有理由使用Directory.Exists 而不是File.Exists
  • 现在可以使用了。我永远不会想到这一点,所以谢谢你随机的陌生人!为你竖起大拇指。

标签: if-statement text-files createfile basic


【解决方案1】:

该文件被其他进程使用,因此不能被覆盖。我建议你先删除文件。

Dim path As String = "put your path"

For Each path In System.IO.Directory.GetFiles("C:\WINDOWS\TEMP")
System.IO.File.Delete(path)
Next path

Dim fs3 As FileStream = File.Create(path)

请确保您拥有[在属性下]文件夹的全部权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 2014-05-18
    • 2022-11-06
    • 2021-07-08
    • 2013-02-06
    • 2022-01-11
    • 2019-08-08
    相关资源
    最近更新 更多