Cloning a csv file to memory and using it in a datatable? (vb.net) - vb.net

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

Related

Access to .txt [VB.NET]

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

multiple programming try to access same text file

I had created and text file using (AFL SCRIPTING LANGUAGE) this script will update (write) to a text file every 5 seconds. I will try to read file using vb.net, when I run the vb.net code form visual studio, everything works fine but (AFL script not able to update the text file), here is my vb.net code:
Dim FILE_NAME As New FileStream("C:\myreport\myfile.TXT", FileMode.Open, FileAccess.Read, FileShare.Read)
REM Dim FILE_NAME As String = "C:\myreport\myfile.TXT"
REM Dim TextLine As String
REM If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
MYSTRING(I) = objReader.ReadLine()
I = I + 1
Loop
REM End If
When I run the code above, ( AFL script not able to update the text file),
Put simply:
When I run the vb.net code (for accessing the text file), AFL script not able to update.
I had share read/write the folder (where the text file exists), no effect, same problem I am facing.
Unchecked "Enable visual studio hosting process", still problem not solved.
In .NET, the FileSystemWatcher class can help you with this problem. You can read the file each time the file watcher says that the files has changed. Here is the reference documentation for it.
You cannot read and write to a file from two processes at the same time. Well, technically you can, but it can lead to a race condition which is bad.
You need to implement some kind of shared locking mechanism around the file to prevent your two programs from fighting over it. Or, if you can guarantee that the consumer VB.NET program will have the file open for less than 5 seconds, you can go with MrAxel's solution and simply have the VB.NET program read the file every time it is updated.

Using the same file error

On a button click I have the following code to write what Is in my textboxes.
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("C:/Users/Nick/Documents/Dra.txt", False)
file.WriteLine(NameBasic)
file.WriteLine(LastBasic)
file.WriteLine(PhoneBasic)
file.WriteLine(NameEmer1)
On my form load, I load what is in the notepad from what was written, It is saying It is already being used(the file) which is true, how can I have two different functions(write, and read) manipulating the same file with out this error?
The process cannot access the file 'C:\Users\Nick\Documents\Dra.txt' because it is being used by another process.
And here is the code for my onformload
Dim read As System.IO.StreamReader
read = My.Computer.FileSystem.OpenTextFileReader("C:/Users/Nick/Documents/Dra.txt")
lblNameBasic.Text = read.ReadLine
I am sort of stuck on this problem, thank you
You need to close the file when you are done writing to it.
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("C:/Users/Nick/Documents/Dra.txt", False)
file.WriteLine(NameBasic)
file.WriteLine(LastBasic)
file.WriteLine(PhoneBasic)
file.WriteLine(NameEmer1)
file.Close()
To answer your question:
how can I have two different functions(write, and read) manipulating the same file with out this error?
If you really want to simultaneously read and write to the same file from two processes, you need to open the file with the FileShare.ReadWrite option. The My.Computer.FileSystem methods don't do that.¹
However, I suspect that you don't really wan't to do that. I suspect that you want to write and, after you are finished writing, you want to read. In that case, just closing the file after using it will suffice. The easiest way to do that is to use the Using statement:
Using file = My.Computer.FileSystem.OpenTextFileWriter(...)
file.WriteLine(...)
file.WriteLine(...)
...
End Using
This ensures that the file will be closed properly as soon as the Using block is left (either by normal execution or by an exception). In fact, it is good practice to wrap use of every object whose class implements IDisposable (such as StreamWriter) in a Using statement.
¹ According to the reference source, OpenTextFileWriter creates a New IO.StreamWriter(file, append, encoding), which in turn creates a new FileStream(..., FileShare.Read, ...).

Write to a text file while is it open and visible

I'm sure this is really simple, but has had me stumped for a while!
I need to show the user a text file, with lines being written to it as my program executes. Stuff like "Working on this file - Successful!". Another forum thread has helped me to allow the text file to be accessed by multiple processes, but now when I open the text file using Process.Start, it doesn't show the lines that are being written using the StreamWriter. Any help very much appreciated.
Code:
Dim ExportLog As String = "C:\ExportLog.txt"
If System.IO.File.Exists(ExportLog) Then
System.IO.File.Delete(ExportLog)
End If
Using writeStream = System.IO.File.Open(ExportLog,
FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.ReadWrite), Write As New StreamWriter(writeStream)
Write.WriteLine("Starting")
End Using
Dim MyLog As Process = Process.Start(ExportLog)

Loading Sequential Files in VB

I have written a vb program a few years ago and now as I get started with vb again i am hitting a "snag". With sequential files. I am trying to load a file in to the vb program with the file dialog box.
NOTE:I am using structures
Dim FileDialog as new openFileDialog
Dim MyStream as Stream = nothing
Dim FileLocation as string 'this is to save the file location
if( FileDialog.ShowDialog() = DialogResults.OK)Then
FS = new FileStream(FileLocation, FileMode.open, fileaccess.Read)
BF = new BinaryFromatter
While FS.Position < FS.Length
Dim temp as unit
...'Please note that this is where the file reads the structures data.It is to much code to write in.
When I run the program I can create a file and save it with the data in and I can load it with the Dialog box, The problem is when I run the program again and try to load it. It just wont run the file or load it(Remember I created the file with this program and saved)
How do I get this to work?
Make sure you have closed the file after writing and reading the data the first time, and make sure you are using the correct path (FileLocation).
Exit Visual Studio between the first and second times you run the program. If it works then, then you know you are not closing the file properly.
Set a breakpoint at the new FileStream assignment and check the value of FileLocation. Is it the same as it was when the file was written?
Check the error message, if there is one, and see if that tells you anything.