I have 2gb above file that file store in file stream using byte, i got the error
"Arithmetic operation resulted in an overflow."
I need to store large file above 2gb.
using this code
Dim fs As System.IO.FileStream
fs = File.Open(fpath, FileMode.Open, FileAccess.Read, FileShare.None)
Dim filelen As Long = fs.Length
Dim buffer(fs.Length - 1) As Byte
fs.Read(buffer, 0, filelen)
fs.Close()
My guess: you are initiating a variable array but the size of the array is over-limit, which would make sense if you are loading a file > 2 Gb to memory (why ?). Check the section "Length Limits" on this page. What you are doing sounds like an awful idea which could even cripple your computer. Instead you should implement some form of streaming, reading the file in smaller chunks.
Related
I use a console app to work on large files. I use
IO.File.ReadAllBytes(OpenFileDlg.FileName)
but it loads all the large file on memory and stops when the memory is full , without reading it completely.
I want to split the large file or read a part of it, "without loading all the file on memory, so it would work on small RAM"
Use FileStream to read in chunks of 4096 bytes then encrypt them (or do any necessary processing) and then write them to the new file stream, since it's streaming in/out it won't hog up all the memory trying to read/write everything at once, simple example:
Dim bytesRead As Integer
Dim buffer(4096) As Byte 'Default buffer size is 4096
Using inFile As New IO.FileStream("C:\\Temp\\inFile.bin", FileMode.Open)
Using outFile As New IO.FileStream("C:\\Temp\\outFile.bin", FileMode.Append, FileAccess.Write)
Do
bytesRead = inFile.Read(buffer, 0, buffer.Length)
If bytesRead > 0 Then
' Do encryption or any necessary processing of the bytes
outFile.Write(buffer, 0, bytesRead)
End If
Loop While bytesRead > 0
End Using
End Using
I have a question about the following code. in order to prevent problems caused by file locking I came across the following code.
Dim OrignalBitmap As New Bitmap(Application.StartupPath & "\IMAGES\BACKGROUND_LARGE.jpg")
Dim CloneBitmap As New Bitmap(OrignalBitmap)
OrignalBitmap.Dispose()
Which works like a charm. Now I have all the images in place and I can still access them as a file without anything locking. It works so well for what I need that I was thinking if its possible to do this for file formats other than images such as Csv files which are then used in a datagridview as a bound table?
Usually it is enough to open a File like this, so that it will not block other programs to access and open it.
Dim path1 As String = "C:\temp\temp.csv"
Using fs As FileStream = File.Open(path1, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
' Do something with filestream
End Using
this will prevent even huge files to open without blocking access
you should check https://learn.microsoft.com/de-de/dotnet/api/system.io.file.open?view=netframework-4.8
Can someone please give me an explanation to this code, and for your information I am trying to save a picture which is displayed in a picture box and save it to the Microsoft access database, I don't understand what anything means, and especially the 0.
If Not PictureBox1.Image Is Nothing Then
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim breader As New BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()
Your snippet code is code to save a binary of picture to imgbuffer variable in VB.Net. This snippet code didn't save this images into Ms Access. But...
I will try to explain it, how this code works :
If Not PictureBox1.Image Is Nothing Then 'This code for checking if there's any images in the picture box, if it's so run next code.
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read) 'This variable declared for getting file name, access file and the read mode. And open it using `Filestream` variable, so after it's opened, the length of images will be saved into `fsreader` variable
Dim breader As New BinaryReader(fsreader) 'This code for reading the binary of images from `fsreader` variable and put the image buffer from range of byte to a variable
Dim imgbuffer(fsreader.Length) As Byte 'This variable is use to collect image buffer with specified length of binary images from `fsreader` variable.
breader.Read(imgbuffer, 0, fsreader.Length) 'This code is use for reading binary of images with specified length of byte and put the image buffer into a imgbuffer variable. The number `0` in the brackets means the binary images will be put fully in imgbuffer. Why `0` ? can I use larger than `0` ? yes you can but the image will be corrupt or differ from the original, so you must read the binary images from 0 to the image file size.
fsreader.Close() 'This is for closing `fsreader` from accessing images file.
So, what's next ?
From that snippet code, you can continued it to process the imgbuffer variable to save it into Ms-Access using some library collection that provided in VB.Net, like OleDB or anything else. At last, I would prefer using MemoryStream to put image buffer and save it into Bitmap variable.
Hope this explain your snippet code.
I've developed a multi-user application. One user saves a string to a text file, other users have to read this string in a background process.
My problem is: when some users open the file to read the string, another user that wants to save a new string can't write to the file. I would like to implement a system that checks if the file is opened or not, and in case it is, waits a few seconds and retries.
What is the function or command to look if the file is already opened?
I solved my problem using tips:
i've replaced this code:
ip_acquisito = My.Computer.FileSystem.ReadAllText(path_elenco & "\ip.txt")
with this code:
Using fs As FileStream = File.Open(path_elenco & "\ip.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim b(1024) As Byte
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Do While fs.Read(b, 0, b.Length) > 0
ip_acquisito = temp.GetString(b)
Loop
End Using
There are quite a few patterns you can deal with. Two I'd recommend would be:
Dealing with FileAccess, FileMode and FileShare
Sync the file access with EventWaitHandle
I am using VB .Net 2008
Why is my conversion from ASCII to 737 (Greek DOS) with the Encoding.Convert command returning readable characters, while an IO.StreamWriter with Encoding.GetEncoding(737) writes a file with non-readable characters?
I am asking this because I want to send row data to a printer which can print Greek as 737.
If I send the result of Encoding.Covert, I get the wrong result, while If I write a file as above and copy it to the printer is ok
Use the GetEncoding method
Dim enc As Encoding = Encoding.GetEncoding(737)
Dim fs As FileStream
fs = File.OpenRead("\737dos.txt")
Dim sr As StreamReader = New StreamReader(fs, enc)