Hello Friends,

I have a patient register form in my project...

When the patient details will be saved in the database it should also be saved in the text file...

I am dynamically creating a folder with patientid and firstname

in that folder I am creating a text file patientreg.txt and it should contain the contents of text file....

I have wriiten the below code but I am getting an error....

Dim di As DirectoryInfo = New DirectoryInfo(Application.StartupPath & "\" + txtPatientID.Text + txtFName.Text)
If Not di.Exists Then
   di.Create()
End If
Dim pathreg As String = di.ToString & "\patientreg.txt"
If File.Exists(pathreg) Then
   Debug.Print("True: " + pathreg)
Else
   File.Create(pathreg)
   Debug.Print("False: " + pathreg)
End If
FileClose()
'Dim objWriter As New System.IO.StreamWriter(pathreg, False)
'objWriter.WriteLine(txtPatientID.Text + "*" + txtFName.Text)
'objWriter.Close()
IO.File.AppendAllText(pathreg, txtPatientID.Text + "*" + txtFName.Text)
Debug.Print("di value: " + di.ToString)

Error: The process cannot access the file 'D:\Pooja\CM\Clinical_Management\Clinical_Management\bin\Debug\435sdfs\patientreg.txt' because it is being used by another process.

Can anyone help me out in this...I also added the code to close the file...but still the error is throwing up....

Dani AI

Generated

The error reported by ("The process cannot access the file ... because it is being used by another process") is almost always a handle leak: calling File.Create(path) opens and returns a FileStream. If that stream is not disposed/closed, the file stays locked. FileClose() only affects VB file numbers opened with FileOpen, so it does not release a .NET FileStream. The FileOpen/Print approach shown later will work, but it mixes old VB file APIs with newer .NET APIs and is less robust.

A safer, modern pattern is to (1) build paths with Path.Combine, (2) create the directory with Directory.CreateDirectory, and (3) write using a Using block so the stream is always disposed. Example (VB.NET):

Dim folder = System.IO.Path.Combine(Application.StartupPath, txtPatientID.Text & txtFName.Text)
System.IO.Directory.CreateDirectory(folder)
Dim pathReg = System.IO.Path.Combine(folder, "patientreg.txt")
Dim record = txtPatientID.Text & "*" & txtFName.Text & System.Environment.NewLine

Using fs As New System.IO.FileStream(pathReg, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Read)
    fs.Seek(0, System.IO.SeekOrigin.End)
    Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(record)
    fs.Write(bytes, 0, bytes.Length)
End Using

Troubleshooting notes and best practices: never call File.Create without disposing the returned stream (call .Dispose() or wrap in Using). Avoid concatenating paths by hand; use Path.Combine. Sanitize user-derived folder names (remove invalid filename chars) and prefer a data folder (e.g., AppData) rather than bin\Debug for persistent files. If locks persist, check for other processes (antivirus, editors, or another thread) and consider opening with an appropriate FileShare mode.

Hello Friends,

I have a patient register form in my project...

When the patient details will be saved in the database it should also be saved in the text file...

I am dynamically creating a folder with patientid and firstname

in that folder I am creating a text file patientreg.txt and it should contain the contents of text file....

I have wriiten the below code but I am getting an error....

Dim di As DirectoryInfo = New DirectoryInfo(Application.StartupPath & "\" + txtPatientID.Text + txtFName.Text)
If Not di.Exists Then
   di.Create()
End If
Dim pathreg As String = di.ToString & "\patientreg.txt"
If File.Exists(pathreg) Then
   Debug.Print("True: " + pathreg)
Else
   File.Create(pathreg)
   Debug.Print("False: " + pathreg)
End If
FileClose()
'Dim objWriter As New System.IO.StreamWriter(pathreg, False)
'objWriter.WriteLine(txtPatientID.Text + "*" + txtFName.Text)
'objWriter.Close()
IO.File.AppendAllText(pathreg, txtPatientID.Text + "*" + txtFName.Text)
Debug.Print("di value: " + di.ToString)

Error: The process cannot access the file 'D:\Pooja\CM\Clinical_Management\Clinical_Management\bin\Debug\435sdfs\patientreg.txt' because it is being used by another process.

Can anyone help me out in this...I also added the code to close the file...but still the error is throwing up....

Got the answer

Dim di As DirectoryInfo = New DirectoryInfo(Application.StartupPath & "\" + txtPatientID.Text + txtFName.Text)
If Not di.Exists Then
    di.Create()
End If
Dim pathreg As String = di.ToString & "\patientreg.txt"
FileOpen(1, pathreg, OpenMode.Output)
If File.Exists(pathreg) Then
    Debug.Print("True: " + pathreg)
Else
    File.Create(pathreg)
    Debug.Print("False: " + pathreg)
End If
Print(1, txtPatientID.Text + "*" + txtFName.Text)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.