VB.NET求助:文件正由另一进程使用,因此该进程无法访问该文件
我用下面的代码先检查有没有该文件存在,若无则创建一个,但是当本地没有该文件时,第一次运行该文件都会报错,提示“文件“D:\Datalog\网络连接测试记录.csv”正由另一进程使用,因此该进程无法访问该文件。”。此时系统应该是在创建该文件,然后程序退出,再然后重新打开这个程序就可以正常使用了。请问怎么解决啊??
下为代码
Sub write()
Dim Line As String
Dim Apath As String = "D:\Datalog\网络连接测试记录.csv "
If File.Exists("D:\Datalog") = False Then
My.Computer.FileSystem.CreateDirectory("D:\Datalog")
End If
If File.Exists(Apath) = False Then
File.Create(Apath)
FileSystem.FileClose()
Dispose()
End If
Line = Chr(13) & Chr(10) & strbian ' 变量
My.Computer.FileSystem.WriteAllText(Apath, Line, True)
FileSystem.FileClose()
End Sub vb.net 创建文件 进程?
[解决办法]
msdn上就有例子
http://msdn.microsoft.com/en-us/library/ms143356.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
:
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText As String = "Hello and Welcome" + Environment.NewLine
File.WriteAllText(path, createText)
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText)
' Open the file to read from.
Dim readText As String = File.ReadAllText(path)
Console.WriteLine(readText)
End Sub
End Class