Visual Basic:检查文件是否存在,如果不存在,则创建文件 [英] Visual Basic: Checking if file exists, if not, create the file

查看:157
本文介绍了Visual Basic:检查文件是否存在,如果不存在,则创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

代码

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

这是我收到的错误消息:

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

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

推荐答案

该文件由其他进程使用,因此无法覆盖。我建议你先删除该文件。

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.

这篇关于Visual Basic:检查文件是否存在,如果不存在,则创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆