Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I keep getting this error on my production server but on my development environment i dont get the error.

I keep getting The Process cannot access the file because it is being used by another process


here is the code i am using

VB
Dim di As New System.IO.DirectoryInfo(Me.Page.Server.MapPath("..\Collection\Data"))
                   Dim aryFi As System.IO.FileInfo() = di.GetFiles("*.txt")
                   Dim fi As System.IO.FileInfo


                   Dim sConnStr As String = ConfigurationManager.AppSettings("Database")
                   Dim cn As New System.Data.SqlClient.SqlConnection(sConnStr)
                   Dim cmdInsertInto As New System.Data.SqlClient.SqlCommand("", cn)
                   cmdInsertInto.CommandType = System.Data.CommandType.Text
                   cmdInsertInto.CommandTimeout = 0
                   Dim count As Integer = 0
                   Dim count2 As Integer = 0
                   For Each fi In aryFi
                       cn.Open()
                       'Opening the file

                       Dim file As New FileStream(fi.FullName, FileMode.Open, FileAccess.Read)
                       'Creating a strem to read the file into text
                       Dim stream As New StreamReader(file)
                       'Read the entire file, split it into line arrays by CRLF, and loop through it.

                       '''''''''''''''''''''CANNOT TRUNCATE !!!!!!!!!!!!! 
 Dim strReqLine As String = stream.ReadLine()

                        Dim fileStrings() As String = fi.Name.ToString.Split(("_").ToCharArray)

                        Dim FileClient As String = fileStrings(0)
                        Dim FileProduct As String = fileStrings(1)
                        Dim FilePostingDate As String = fileStrings(2).TrimEnd((".txt").ToCharArray)

                       While Not strReqLine Is Nothing
                           cmdInsertInto.CommandText = "INSERT INTO BulkImportHoldingData (ClientName,Product,PostingDate,BulkColumn) VALUES ('" & FileClient & "','" & FileProduct & "','" & FilePostingDate & "','" & strReqLine.Replace("'", "") & "')"
                           cmdInsertInto.ExecuteNonQuery()
                           strReqLine = stream.ReadLine()
                           count += 1

                           con.Open()
                           Cmd = New SqlCommand("Update Progress set cnt = '" & count & "'", con)
                           Cmd.ExecuteNonQuery()
                           con.Close()
                       End While

                       stream.Close()

                       'MsgBox("File " & fi.FullName & " has been prepaired", MsgBoxStyle.Information, "Your Request is being Processed")

                       fi.MoveTo(fi.FullName.TrimEnd((".txt").ToCharArray) & ".old")
                       count2 += 1
                       cn.Close()
                   Next



Any idea what is causing this error, it reads about 25 out of 30 files in the folder and then gives me the error, the file sizes range between 8kb and 193kb. Any ideas?
Posted
Updated 2-Dec-13 3:58am
v4
Comments
ZurdoDev 2-Dec-13 11:42am    
What line of code causes the error?
isi19 3-Dec-13 1:02am    
When i debug on my development i dont get any errors, the process runs smoothly

1 solution

Its not your code that is the problem. Its just what your error message tells you. "The Process cannot access the file because it is being used by another process". All this meanss is that another program is already using the file you are trying to access. You need to stop that process before your program will be able to do anything to that file.
 
Share this answer
 
Comments
isi19 3-Dec-13 1:01am    
But nothing else is running when i run this process and the only person with access to that folder that the file is being read from on the server is me,and the file was not opened.
isi19 3-Dec-13 1:49am    
The main issue i am having is it is getting a a file which is 193kb, it reads the file and once the file is completed being read it is creating a temp file, so if the file is file1.txt it then is creating a file called file1.txt.tmp

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900