Vb.net 如何解决这个问题。进程无法访问该文件
进程无法访问文件“F:\copy back\system\HRM 2-5-2013\HRM\HRM\lanbase.txt”,因为另一进程正在使用该文件。 这是我在sub main中的代码Vb.net 如何解决这个问题。进程无法访问该文件,vb.net,Vb.net,进程无法访问文件“F:\copy back\system\HRM 2-5-2013\HRM\HRM\lanbase.txt”,因为另一进程正在使用该文件。 这是我在sub main中的代码 Public localhost As String Public username As String Public port As String Public database As String Public conn As New MySqlConnection Public NAME1 As Str
Public localhost As String
Public username As String
Public port As String
Public database As String
Public conn As New MySqlConnection
Public NAME1 As String = "F:\copy back up\system\HRM 2-5-2013\HRM\HRM\lanbased.txt"
Public Sub main()
Dim frm As New Form1
Dim frm1 As New create
If System.IO.File.Exists(NAME1) = True Then
Try
Dim objReader As New System.IO.StreamReader(NAME1)
localhost = objReader.ReadLine() & vbNewLine
username = objReader.ReadLine() & vbNewLine
port = objReader.ReadLine() & vbNewLine
database = objReader.ReadLine() & vbNewLine
conn.ConnectionString = "server=" & Trim(localhost) & ";user id=" & Trim(username) & "; password=" & Trim(port) & "; database=" & Trim(database) & ""
conn.Open()
Application.Run(New Form1())
Catch ex As Exception
MsgBox("Unable to connect to database", vbCritical)
Application.Run(New create())
End Try
End If
Exit Sub
End Sub
这是我在表单create中的代码。
当另一个进程正在使用该文件时,如何访问该文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "F:\copy back up\system\HRM 2-5-2013\HRM\HRM\lanbased.txt"
If TextBox1.Text = Nothing Or TextBox2.Text = Nothing Or TextBox3.Text = Nothing Or TextBox4.Text = Nothing Then
MsgBox("fill up mo pa ngot")
ElseIf System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write(TextBox1.Text + vbCrLf)
objWriter.Write(TextBox2.Text + vbCrLf)
objWriter.Write(TextBox3.Text + vbCrLf)
objWriter.Write(TextBox4.Text + vbCrLf)
objWriter.Close()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
ElseIf conn.State = True Then
MsgBox("maka connect naka")
End If
End Sub
看起来您需要在打开新表单之前关闭streamReader:
objReader.Close()
这将释放文件 首先在此处打开文件进行阅读:
Dim objReader As New System.IO.StreamReader(NAME1) //1st open
然后调用form1:应用程序。运行(New form1())
在这种形式下,您有:Dim objWriter As New System.IO.StreamWriter(文件名)//2nd open
但是等待您没有关闭您的文件
,因此您无法第二次打开它进行写入
因此,在调用create form 1之前,您需要关闭文件,如objReader.close()
conn.Open()
objReader.close()检查此可执行文件是否运行了两次(在任务管理器中)no。。问题出在代码中。。文件F:\copy back up\system\HRM 2-5-2013\HRM\HRM\lanbased.txt被使用了两次。我如何才能使用此文件两次。?SysDragon我使用了您的代码,但仍然存在相同的问题:((现在我遇到此错误:()无法连接到任何指定的MySQL主机。
conn.Open()
objReader.close() <----- this one
Application.Run(New Form1())