Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Being used by another process error

  1. #1

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Resolved [RESOLVED] [2005] Being used by another process error

    The problem seems to be with a text file that I'm accessing. I can't figure out where the problem might be. From the code i'm getting errors at
    Error Place 4 and 5 (you will see what i mean by that below). The message says that the file

    (Application.StartupPath & "\UserHistory.txt")

    is being used by another process but I've checked and this is the only code that can access the text file in question. Anyone spot what's the
    matter with it? Here's the relevant code:

    VB Code:
    1. Try
    2.             If System.IO.File.Exists(Application.StartupPath & "\UserHistory.txt") = False Then
    3.                 System.IO.File.Create(Application.StartupPath & "\UserHistory.txt") 'CREATE FILE
    4.             End If     'DO I NEED TO RELEASE THE FILE OR SOMETHING??
    5.         Catch ex As Exception
    6.             MessageBox.Show(ex.Message & "ERROR PLACE 1")
    7.         End Try
    8.  
    9. 'THERE'S CODE HERE TO CHECK THAT ALL FIELDS HAVE BEEN FILLED IN
    10. 'THEN...
    11.  
    12. 'Once TRUE create User Account
    13.         'FIRST CREATE USER FOLDER:
    14.         'DEFINE THE USER FOLDER PATH TO CHECK/CREATE
    15.         Dim userPath As String = Application.StartupPath & "\User Accounts\" & _
    16.         txtSurname.Text.ToLower & "\" & txtCreateUsername.Text.ToLower & "\"
    17.  
    18.         Try
    19.             If Not My.Computer.FileSystem.DirectoryExists(userPath) Then
    20.                 'CREATE FOLDER
    21.                My.Computer.FileSystem.CreateDirectory(userPath)
    22.             ElseIf My.Computer.FileSystem.DirectoryExists(userPath) Then
    23.                 'Check Sub Directory Text File
    24.                 MessageBox.Show("An account with these details already exists." & _
    25.                 "Please select a new username or use the account details finder to" & _
    26.                 "get your existing details", "Account already exists", MessageBoxButtons.OK)
    27.                 txtCreateUsername.Clear()    'Clear & reselect
    28.                 txtCreateUsername.Select()   'username field
    29.                 Exit Sub
    30.             End If
    31.         Catch ex As Exception
    32.             MessageBox.Show(ex.Message & "ERROR PLACE 2")
    33.         End Try
    34.  
    35.         'Once check has been made, create a text file that
    36.         'will contain all the  user details:
    37.         Try
    38.             'Create STREAMWRITER
    39.             Dim sw As New System.IO.StreamWriter(userPath & "\userDetails.txt")
    40.             Dim UserDOB As Date = CDate(cboDay.Text & "/" & cboMonth.Text & "/" & cboYear.Text)
    41.             sw.Write("Surname:" & txtSurname.Text.ToLower & Environment.NewLine)
    42.             sw.Write("First Name:" & txtFirstName.Text.ToLower & Environment.NewLine)
    43.             sw.Write("Occupation:" & cboOccupation.Text.ToLower & Environment.NewLine)
    44.             sw.Write("DOB:" & UserDOB & Environment.NewLine)
    45.             sw.Write("Username:" & txtCreateUsername.Text.ToLower & Environment.NewLine)
    46.             sw.Write("Password:" & txtCreatePassword.Text.ToLower & Environment.NewLine)
    47.             sw.Write("Question:" & cboPWquestion.SelectedItem.ToString & Environment.NewLine) 'SELECTED SECURITY QUESTION
    48.             sw.Write("Answer:" & txtPWAnswer.Text.ToLower & Environment.NewLine) 'ANSWER TO SECURITY QUESTION
    49.             sw.Close()        'CLOSE & DISPOSE OF STREAMWRITER
    50.             sw.Dispose()
    51.         Catch ex As Exception
    52.             MessageBox.Show(ex.Message & "ERROR PLACE 3")
    53.         End Try
    54.  
    55.         'ADD CUURENT USER SURNAME TO A LOG FILE OF SURNAMES:
    56.         'BEFORE ADDING, CHECK FOR DUPLICATE
    57.         Try
    58.             'FIRSTLY, READ EXISTING NAMES FROM LOG FILE
    59.             Dim srNew As New System.IO.StreamReader(Application.StartupPath & "\UserHistory.txt")
    60.             If srNew.Peek = -1 Then Exit Try 'Check if text file has text in it
    61.             Do While srNew.Peek <> -1
    62.                 arr.Add(srNew.ReadLine)
    63.             Loop
    64.             srNew.Close()
    65.             srNew.Dispose()
    66.             arr.Sort()
    67.         Catch ex As Exception
    68.             MessageBox.Show(ex.Message & "ERROR PLACE 4")
    69.         End Try
    70.  
    71.         Try   'NOW CHECK AGAINST TEXT ENTERED INTO SURNAME FIELD
    72.             For Each item As String In arr
    73.                 If txtSurname.Text.ToLower = item Then
    74.                     MessageBox.Show("Surname already exists.")
    75.                     Exit Try
    76.                 End If
    77.             Next
    78.             My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\UserHistory.txt", _
    79.           Environment.NewLine & txtSurname.Text.ToLower, True)
    80.         Catch ex As Exception
    81.             MessageBox.Show(ex.Message & "ERROR PLACE 5")
    82.         End Try
    83.  
    84.         MessageBox.Show("Congratulations! Account created successfully! You can now log on.", "Account Created", MessageBoxButtons.OK)
    85.         Me.grPersonal.Visible = False
    86.         Me.grpPassword.Visible = False
    87.         pnlLogin.Show()
    88.         Call FillUserHistory()
    Last edited by stimbo; Oct 5th, 2006 at 07:38 AM.

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: [2005] Being used by another process error

    I think when you create the file up top, it is actually a function that returns some sort of stream. You should assign it to a variable and then close it.

  3. #3

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Being used by another process error

    I'm pretty sure that's where the problem lies.

    I have gone through every line and when the file exists there isn't a problem. I had made a comment in the code about releasing the file after it's created as a possible reason(or whatever the term is). I think that's the case.

    Specifying the filename as a String variable doesn't seem to fix the problem though.

    EDIT - I moved the code to the form_Load event (instead of within button click) and it seems to work okay. At this stage, that will do for me! I'll mark it resolved but if anyone can explain how to fix this properly then please feel free to reply.

    Stim
    Last edited by stimbo; Oct 5th, 2006 at 09:12 AM.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [RESOLVED] [2005] Being used by another process error

    File.Create(filePath) doesn't just create the file but also opens it to return a filestream. So if you just want to create the file without writing anything to it then you must close the filestream to release that file for later use.
    Your code should be changed to:
    VB Code:
    1. If System.IO.File.Exists(Application.StartupPath & "\UserHistory.txt") = False Then
    2.                 System.IO.File.Create(Application.StartupPath & "\UserHistory.txt").Close() 'CREATE FILE THEN CLOSE IT
    3.             End If

  5. #5

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [RESOLVED] [2005] Being used by another process error

    Ahh, that's what I've been looking for.

    I was wondering how to close the stream. Usually with Streamreader for example you can just do something like sr.close

    I didn't know you could just add .Close to the end of File.Create(filehere)

    Live and learn.

    Thanks,

    Stim.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width