Visual Basic: Checking if file exists, if not, create the file

心不动则不痛 提交于 2020-01-06 14:17:38

问题


This is the code. It checks if the file in path exists, if not, it creates the file. I'm getting this error message all the time and I don't know why. Maybe I should close the System.IO.Directory.Exists? If yes, how do I do that? Just so you know, I'm creating a text file.

The code

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

This is the error message I get:

Process can't use the file (path) because some other process is using this file at the moment.


回答1:


The file is used by other processes hence it can't be overwritten. I suggest you delete the file first.

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)

Be certain that you have full rights [under properties] to the folder.



来源:https://stackoverflow.com/questions/22936426/visual-basic-checking-if-file-exists-if-not-create-the-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!