Getting Cannot access a closed file error - vb.net

I use the following code to iterate a collection of files that I need to copy from one folder to another. It works fine when the source file exist, but when it does not exist I get
System.ObjectDisposedException: Cannot access a closed file. at System.IO.__Error.FileNotOpen() at System.IO.FileStream.get_Position()
What am I missing here?
For Each itm In listOfFiles
Try
If File.Exists(itm.SourcePath + itm.FileName) Then
Dim cf As New FileStream(itm.SourcePath + itm.FileName, FileMode.Open)
Dim ct As New FileStream(itm.DestinationPath + itm.FileName, FileMode.Create)
Dim len As Long = cf.Length - 1
Dim buffer(1024) As Byte
Dim byteCFead As Integer
While cf.Position < len
byteCFead = (cf.Read(buffer, 0, 1024))
ct.Write(buffer, 0, byteCFead)
fileCopyProgressBar.BeginInvoke(New Action(Sub() fileCopyProgressBar.Value = CInt(cf.Position / len * 100)))
End While
ct.Flush()
ct.Close()
cf.Close()
itm.FileExsits = True
Else
itm.FileExsits = False
End If
Catch ex As Exception
log.Error(ex.Message & " (unc)")
End Try
Next

Try calculating value before putting it in the action. You should also dispose of the streams when done with them
For Each itm In listOfFiles
Try
If File.Exists(itm.SourcePath + itm.FileName) Then
Using cf As New FileStream(itm.SourcePath + itm.FileName, FileMode.Open)
Using ct As New FileStream(itm.DestinationPath + itm.FileName, FileMode.Create)
Dim len As Long = cf.Length - 1
Dim buffer(1024) As Byte
Dim byteCFead As Integer
Dim percentage As Integer
While cf.Position < len
byteCFead =(cf.Read(buffer, 0, 1024))
ct.Write(buffer, 0, byteCFead)
percentage = CInt(cf.Position / len * 100)
fileCopyProgressBar.BeginInvoke(New Action(Sub() fileCopyProgressBar.Value = percentage))
End While
ct.Flush()
ct.Close()
cf.Close()
End Using
End Using
itm.FileExsits = True
Else
itm.FileExsits = False
End If
Catch ex As Exception
log.Error(ex.Message & " (unc)")
End Try
Next

Related

Performance improvement on vb.net code

I need to write 50 million records with 72 columns into text file, the file size is growing as 9.7gb .
I need to check each and every column length need to format as according to the length as defined in XML file.
Reading records from oracle one by one and checking the format and writing into text file.
To write 5 crores records it is taking more than 24 hours. how to increase the performance in the below code.
Dim valString As String = Nothing
Dim valName As String = Nothing
Dim valLength As String = Nothing
Dim valDataType As String = Nothing
Dim validationsArray As ArrayList = GetValidations(Directory.GetCurrentDirectory() + "\ReportFormat.xml")
Console.WriteLine("passed xml")
Dim k As Integer = 1
Try
Console.WriteLine(System.DateTime.Now())
Dim selectSql As String = "select * from table where
" record_date >= To_Date('01-01-2014','DD-MM-YYYY') and record_date <= To_Date('31-12-2014','DD-MM-YYYY')"
Dim dataTable As New DataTable
Dim oracleAccess As New OracleConnection(System.Configuration.ConfigurationManager.AppSettings("OracleConnection"))
Dim cmd As New OracleCommand()
cmd.Connection = oracleAccess
cmd.CommandType = CommandType.Text
cmd.CommandText = selectSql
oracleAccess.Open()
Dim Tablecolumns As New DataTable()
Using oracleAccess
Using writer = New StreamWriter(Directory.GetCurrentDirectory() + "\FileName.txt")
Using odr As OracleDataReader = cmd.ExecuteReader()
Dim sbHeaderData As New StringBuilder
For i As Integer = 0 To odr.FieldCount - 1
sbHeaderData.Append(odr.GetName(i))
sbHeaderData.Append("|")
Next
writer.WriteLine(sbHeaderData)
While odr.Read()
Dim sbColumnData As New StringBuilder
Dim values(odr.FieldCount - 1) As Object
Dim fieldCount As Integer = odr.GetValues(values)
For i As Integer = 0 To fieldCount - 1
Dim vals As Array = validationsArray(i).ToString.ToUpper.Split("|")
valName = vals(0).trim
valDataType = vals(1).trim
valLength = vals(2).trim
Select Case valDataType
Case "VARCHAR2"
If values(i).ToString().Length = valLength Then
sbColumnData.Append(values(i).ToString())
'sbColumnData.Append("|")
ElseIf values(i).ToString().Length > valLength Then
sbColumnData.Append(values(i).ToString().Substring(0, valLength))
'sbColumnData.Append("|")
Else
sbColumnData.Append(values(i).ToString().PadRight(valLength))
'sbColumnData.Append("|")
End If
Case "NUMERIC"
valLength = valLength.Substring(0, valLength.IndexOf(","))
If values(i).ToString().Length = valLength Then
sbColumnData.Append(values(i).ToString())
'sbColumnData.Append("|")
Else
sbColumnData.Append(values(i).ToString().PadLeft(valLength, "0"c))
'sbColumnData.Append("|")
End If
'sbColumnData.Append((values(i).ToString()))
End Select
Next
writer.WriteLine(sbColumnData)
k = k + 1
Console.WriteLine(k)
End While
End Using
writer.WriteLine(System.DateTime.Now())
End Using
End Using
Console.WriteLine(System.DateTime.Now())
'Dim Adpt As New OracleDataAdapter(selectSql, oracleAccess)
'Adpt.Fill(dataTable)
Return Tablecolumns
Catch ex As Exception
Console.WriteLine(System.DateTime.Now())
Console.WriteLine("Error: " & ex.Message)
Console.ReadLine()
Return Nothing
End Try

i'm trying to load a text file into an array. in vb

this is what i have so far
the array size is (199,2)
the file holds lines that look like this: Canada,34026478,12938494
USA,3377786,5345678
Sub LoadFromFile(filename As String)
Dim Reader As StreamReader = File.OpenText(filename)
Dim Currentline As String
Dim lineArray() As String
Do While Reader.EndOfStream = False
Currentline = Reader.ReadLine
lineArray = Currentline.Split(","c)
Loop
Using sr As StreamReader = New StreamReader(filename)
Do While sr.Peek() >= 0
For Each c As Char In sr.ReadLine.Split
Try
ValuesFromFile(y, x) = CStr(Integer.Parse(c))
Catch ex As Exception 'i assume this is the only possible error, but we could be out of bounds due to assuming the actual size of the file/line... catch specific exceptions as necessary'
End Try
y += 1
Next
x += 1
y = 0
Loop
End Using
Reader.Close()
End Sub

system.io.ioexception the process cannot access because it is being used by another process

i am getting this problem in some systems, some systems working properly, here my code is,
Dim fileName As String = "FaultTypesByMonth.csv"
Using writer As IO.StreamWriter = New IO.StreamWriter(fileName, True, System.Text.Encoding.Default) '------------ rao new ----
Dim Str As String
Dim i As Integer
Dim j As Integer
Dim headertext1(rsTerms.Columns.Count) As String
Dim k As Integer = 0
Dim arrcols As String = Nothing
For Each column As DataColumn In TempTab.Columns
arrcols += column.ColumnName.ToString() + ","c
k += 1
Next
writer.WriteLine(arrcols)
For i = 0 To (TempTab.Rows.Count - 1)
For j = 0 To (TempTab.Columns.Count - 1)
If j = (TempTab.Columns.Count - 1) Then
Str = (TempTab.Rows(i)(j).ToString)
Else
Str = (TempTab.Rows(i)(j).ToString & ",")
End If
writer.Write(Str)
Next
writer.WriteLine()
Next
writer.Close()
writer.Dispose()
End Using
Dim FileToDelete As String = Nothing
Dim sd As New SaveFileDialog
sd.Filter = "CSV Files (*.csv)|*.csv"
sd.FileName = "FaultTypesByMonth"
If sd.ShowDialog = Windows.Forms.DialogResult.OK Then
FileCopy(fileName, sd.FileName)
MsgBox(" File Saved in selected path")
FileToDelete = fileName
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
FileToDelete = fileName
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
when i am trying to save this file in desired path, then i am getting this error.
if save in shared folder i am not getting this error
system.io.ioexception the process cannot access because it is being used by another process...
what i am doing wrong,Help me

Read/Write binary files

I have this code which works well:
Public Function LoadBinaryFile(strFilename As String) As Byte()
Using fsSource As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read)
' Read the source file into a byte array.
Dim bytes() As Byte = New Byte((fsSource.Length) - 1) {}
Dim numBytesToRead As Integer = CType(fsSource.Length, Integer)
Dim numBytesRead As Integer = 0
'tsProgressBar.Minimum = 0
'tsProgressBar.Maximum = numBytesToRead
While (numBytesToRead > 0)
' Read may return anything from 0 to numBytesToRead.
Dim n As Integer = fsSource.Read(bytes, numBytesRead, _
numBytesToRead)
' Break when the end of the file is reached.
If (n = 0) Then
Exit While
End If
numBytesRead = (numBytesRead + n)
numBytesToRead = (numBytesToRead - n)
'tsProgressBar.Value = numBytesRead
End While
numBytesToRead = bytes.Length
Return bytes
End Using
End Function
And I have this code to save the file which also works well:
Public Function SaveBinaryFile(strFilename As String, bytesToWrite() As Byte) As Boolean
Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write)
fsNew.Write(bytesToWrite, 0, bytesToWrite.Length)
End Using
End Function
What I am after is some help to modify the SaveBinaryFile function to implement a progress bar.
Final:
OK, I have written the function myself. Here it is:
Public Function ReadBinaryFile(strFilename As String) As Byte()
Dim position As Integer = 0
Dim bufferSize As Integer = 4096
Dim bytes() As Byte
'frmMain.tsProgressBar.Value = 0
Using fsOpen As FileStream = New FileStream(strFilename, FileMode.Open)
redim bytes((fsOpen.Length) - 1)
Do
If (position + bufferSize) > fsOpen.Length Then
fsOpen.Read(bytes, position, fsOpen.Length - position)
Exit Do
Else
fsOpen.Read(bytes, position, bufferSize)
End If
'frmMain.tsProgressBar.Value = ((position / fsOpen.Length) * 100)
'frmMain.tsProgressBar.Refresh()
Application.DoEvents()
position += bufferSize
Loop
End Using
Return bytes
End Function
My.Computer.Filesystem.ReadAllBytes("filename") reads the entire file into a byte array.
My.Computer.Filesystem.WriteAllBytes("filename", bytes, false) writes it back.
I've never tried using the Async options on FileStream but there don't seem to be any event handlers.
I would break it down into a loop, you have the total amount of bytes to write so you could loop through writing 4k at a time, update your progress bar and continue the loop.
Public Sub SaveBinaryFile(strFilename As String, bytesToWrite() As Byte)
Dim position As Integer = 0
Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write)
Do
Dim intToCopy As Integer = Math.Min(4096, bytesToWrite.Length - position)
Dim buffer(intToCopy - 1) As Byte
Array.Copy(bytesToWrite, position, buffer, 0, intToCopy)
fsNew.Write(buffer, 0, buffer.Length)
ProgressBar1.Value = ((position / bytesToWrite.Length) * 100)
Application.DoEvents()
position += intToCopy
Loop While position < bytesToWrite.Length
End Using
End Sub
'Or, if you are reading a file from a URL, here is a way to read the file into an array of bytes.
Dim WebRequest As Net.HttpWebRequest = Net.WebRequest.Create("http://mypage.abc.com/myfolder/MyFileName.xls")
Using WBinReader As BinaryReader = New BinaryReader(WRequest.GetResponse.GetResponseStream)
Dim file_buffer() As Byte
'10000000 is an arbitrary number, but File_Buffer will have no more
'elements than they number of bytes in the URL's file.
file_buffer = WBinReader.ReadBytes(10000000)
file_bytes =UBound(buffer)+1
End Using
Brian Jasmer

How to read XML data from a URL by using vb.NET and save

Friends, I am able to get XML file by sing bytes, perhaps which is getting some problem. Can u suggest me alternate method to do the same thing to save XML file?
Try
Dim strUrl As String = "http://example.com"
Dim wr As HttpWebRequest = CType(WebRequest.Create(strUrl), HttpWebRequest)
Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
ws.ContentType = "UTF-16"
Dim str As Stream = ws.GetResponseStream()
Dim inBuf(100000) As Byte
Dim bytesToRead As Integer = CInt(inBuf.Length)
Dim bytesRead As Integer = 0
While bytesToRead > 0
Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
If n = 0 Then
Exit While
End If
bytesRead += n
bytesToRead -= n
End While
Dim fstr As New FileStream("c:/GetXml.xml", FileMode.OpenOrCreate, FileAccess.Write)
fstr.Write(inBuf, 0, bytesRead)
str.Close()
fstr.Close()
Catch ex As WebException
Response.Write(ex.Message)
End Try
Why not just use the WebClient class and its DownloadFile method?? Seems a lot easier....
This is in C#, but you should have no trouble converting that to VB.NET:
WebClient wc = new WebClient();
wc.DownloadFile("http://xyz", #"C:\getxml.xml");
and you're done!
Marc
Consider using XMLTextReader. This example just loads the entire XML into a string, but obviously you could write it to a file instead:
Dim strUrl As String = "http://xyz.com"
Dim reader As XmlTextReader = New XmlTextReader(strUrl)
Dim output as String
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element
Output = Output + "<" + reader.Name
If reader.HasAttributes Then
While reader.MoveToNextAttribute()
Output = Output + " {0}='{1}'", reader.Name, reader.Value)
End While
End If
Output = Output + ">"
Case XmlNodeType.Text
Output = Output + reader.Value
Case XmlNodeType.EndElement
Output = Output + "</" + reader.Name + ">"
End Select
Loop
What if the service is sending the request to our URL? How do I adjust this to read the http stream they send? Having such a hard time... (Should I do a separate thread? Sorry.)