vb.net 如何解决这个问题。进程无法访问文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14997606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:42:05  来源:igfitidea点击:

How to solve this . The process cannot access the file

vb.net

提问by Rodmar Pusung

The process cannot access the file 'F:\copy back up\system\HRM 2-5-2013\HRM\HRM\lanbased.txt' because it is being used by another process. This is my code in sub main

该进程无法访问文件“F:\copy back up\system\HRM 2-5-2013\HRM\HRM\lanbased.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 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

and this is my code in my form create. How do I access the file when it is being used by another process?

这是我在表单中创建的代码。另一个进程正在使用该文件时如何访问该文件?

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

采纳答案by Arpit

first you open your file for reading here :

首先你打开你的文件在这里阅读:

Dim objReader As New System.IO.StreamReader(NAME1)    //1st open

Second you call the form1 : Application.Run(New Form1())

其次,您调用 form1 : Application.Run(New Form1())

in that Form you have : Dim objWriter As New System.IO.StreamWriter(FILE_NAME) //2nd open

在该表格中,您有: Dim objWriter As New System.IO.StreamWriter(FILE_NAME) //2nd open

But wait you didn't close your fileso you can't open it 2nd time for writing.

但是等等,you didn't close your file这样你就不能第二次打开它来写了。

So you need to close the file before calling create form 1like objReader.close()

所以你需要before calling create form 1像这样关闭文件objReader.close()

        conn.Open()
        objReader.close()    <----- this one
        Application.Run(New Form1())

回答by SysDragon

Looks like you need to close your streamReader before opening the new form:

看起来您需要在打开新表单之前关闭 streamReader:

objReader.Close()

That will free the file.

这将释放文件。