| | |
Help for file handling in VB. NET
Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
![]() |
Hi all,
I am trying to make a program in VB .NET that has two "browse" bottons, two textboxes which should show the full path of the files to be selected, one "Save As" button, one textbox that should show the file's full path where to be saved in, an "OK" button, and a "Cancel" button. By the way files are binary.
When the program starts executing, the user will select (by using open file dialog) two different binary files(file1 and file2 for example). Then the user should give a name to save the file and select the directory where to be saved by clicking Save As button.. Then the user should click Ok button. Once the OK button clicked, first value (colum1,row1) of file1 should be added to first value of file2 (colum1,row1) and this will continue till the end of both files. Once the addition ends, the resulted output file should be saved into a directory where the user prompts. Is this program very hard to code for a beginner? Or does anyone know where I can get a similar code?
I would greatly appreciate any help.
I am trying to make a program in VB .NET that has two "browse" bottons, two textboxes which should show the full path of the files to be selected, one "Save As" button, one textbox that should show the file's full path where to be saved in, an "OK" button, and a "Cancel" button. By the way files are binary.
When the program starts executing, the user will select (by using open file dialog) two different binary files(file1 and file2 for example). Then the user should give a name to save the file and select the directory where to be saved by clicking Save As button.. Then the user should click Ok button. Once the OK button clicked, first value (colum1,row1) of file1 should be added to first value of file2 (colum1,row1) and this will continue till the end of both files. Once the addition ends, the resulted output file should be saved into a directory where the user prompts. Is this program very hard to code for a beginner? Or does anyone know where I can get a similar code?
I would greatly appreciate any help.
•
•
Join Date: Nov 2007
Posts: 37
Reputation:
Solved Threads: 4
The column1, row1 stuff kind of confusses me but I think that you are wanting to read a val from file1 and another from file2 and put the resulting value into another file. If so you would use a loop to read file1 to the end while getting a line from file2. I haven't tested this but I believe that it could work or at least get you started, I'm assuming that the length of both files are the same;
VB.NET Syntax (Toggle Plain Text)
'make a file stream for writing out the value Dim fs As New System.IO.FileStream("yourfile.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write) 'make a stream to hold the values Dim streamOut As New System.IO.BinaryWriter(fs) Dim srFile1 As New System.IO.FileStream("file1.txt", IO.FileMode.Open) Dim srFile2 As New System.IO.FileStream("file2.txt", IO.FileMode.Open) 'create readers to get the values Dim streamFile1 As New System.IO.BinaryReader(srFile1) Dim streamFile2 As New System.IO.BinaryReader(srFile2) 'vars to hold values Dim iVal1, iVal2, iVal3 As Integer Try 'read from the first file to the end For intJ As Integer = 0 To streamFile1.BaseStream.Length - 1 'get values iVal1 = streamFile1.ReadInt32 iVal2 = streamFile2.ReadInt32 'put vals together iVal3 = iVal1 + iVal2 'write to the output file streamOut.Write(iVal3) Next 'close the files streamFile1.Close() streamFile2.Close() streamOut.Close() Catch ex As Exception Throw ex End Try
Thanks emurf for the code you posted. I have another question. If the user clicks OK button first instead of loading input files, I get an error filenotfoundexception error. I am trying to add a message saying "Please enter a file first" and textbox1 should be focused and should be waiting for the user to enter a file. Below my code simply dies when I click OK button without selecting input file first.
'Load/Select input file button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
openFD.InitialDirectory = "C:\"
openFD.Multiselect = False
openFD.Title = "Select first image file"
openFD.Filter = "All Files [*.*]| *.*"
' Make sure the User clicked OK and not Cancel
If (openFD.ShowDialog() = Windows.Forms.DialogResult.OK) Then
TextBox1.Text = ""
Dim loadFileName As String = openFD.FileName
'show the full path of selected file
TextBox1.AppendText(loadFileName)
End If
End Sub
'Ok button code
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Call testRun()
Me.Close()
End Sub
Public Function testRun()
Dim s1 As FileStream
Dim s2 As FileStream
'---read from and write to a binary file
s1 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
s2 = New FileStream(saveFD.FileName, FileMode.CreateNew, FileAccess.Write)
Dim br As BinaryReader
Dim bw As BinaryWriter
br = New BinaryReader(s1)
bw = New BinaryWriter(s2)
Dim byteRead As Byte
Dim j As Integer
For j = 0 To br.BaseStream.Length() - 1
byteRead = br.ReadByte
bw.Write(byteRead)
Next
br.Close()
bw.Close()
Return byteRead
End Function
'Load/Select input file button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
openFD.InitialDirectory = "C:\"
openFD.Multiselect = False
openFD.Title = "Select first image file"
openFD.Filter = "All Files [*.*]| *.*"
' Make sure the User clicked OK and not Cancel
If (openFD.ShowDialog() = Windows.Forms.DialogResult.OK) Then
TextBox1.Text = ""
Dim loadFileName As String = openFD.FileName
'show the full path of selected file
TextBox1.AppendText(loadFileName)
End If
End Sub
'Ok button code
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Call testRun()
Me.Close()
End Sub
Public Function testRun()
Dim s1 As FileStream
Dim s2 As FileStream
'---read from and write to a binary file
s1 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
s2 = New FileStream(saveFD.FileName, FileMode.CreateNew, FileAccess.Write)
Dim br As BinaryReader
Dim bw As BinaryWriter
br = New BinaryReader(s1)
bw = New BinaryWriter(s2)
Dim byteRead As Byte
Dim j As Integer
For j = 0 To br.BaseStream.Length() - 1
byteRead = br.ReadByte
bw.Write(byteRead)
Next
br.Close()
bw.Close()
Return byteRead
End Function
•
•
Join Date: Nov 2007
Posts: 37
Reputation:
Solved Threads: 4
There are two ways to check the input. One way is to make sure that there is a string in the textbox and the other is to make sure that the filepath exists. The look something like this;
VB.NET Syntax (Toggle Plain Text)
'Make sure that something is in the textbox If Len(Me.Textbox1.Text.Trim) = 0 Then MessageBox.Show("Please enter a file first", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 'set focus to textbox1 Me.Textbox1.Focus End If 'This validates the filepath, done after you check to make sure that the user entered someting in textbox1 If Not System.IO.File.Exists(Me.Textbox1.Text.Trim) Then MessageBox.Show("The file entered is invalid", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 'set focus to textbox1 Me.Textbox1.Focus End If
Hi guys
In a standart save file dialog, if the filename exists during a saving process, I select overwrite button but it throws filealreadyexists exception and program dies. How could I make that when the user clicks overwrite button the program will not die and it would actually overwrite the previosly existing filename? Thanks.
In a standart save file dialog, if the filename exists during a saving process, I select overwrite button but it throws filealreadyexists exception and program dies. How could I make that when the user clicks overwrite button the program will not die and it would actually overwrite the previosly existing filename? Thanks.
•
•
Join Date: Nov 2007
Posts: 37
Reputation:
Solved Threads: 4
You would do that with the stream writer object like this,
If for some reason that doesn't work or you just cant do that, you may not be able to in the code that I wrote above, you cand do this
Dim swWriter As New System.IO.StreamWriter("textfile.txt", False). The second parameter is append. If it is false then the file is overwritten, if it is true then what you are writing is added to the end of the file. If for some reason that doesn't work or you just cant do that, you may not be able to in the code that I wrote above, you cand do this
If System.IO.File.Exists("filepath") Then System.IO.File.Delete("filepath") before you create and write your file. Hi emurf. My input files are single-band satellite images where each pixel has a value of between 0 and 255. As you know I use binaryreader and binarywriter objects to read and write to a file. Up tu this point everything is ok. The point I can't understand is what to do right after the input files are read. I am supposed to apply this simple division: (inputfile1/inputfile2) that is each pixel value of inputfile1 will be divided by corresponding pixel value of inputfile2. Here are 2 points where I am stuck with:
1-Would it make sense to create a 2-dimensional array for each inputfile and read them using binaryreader and binarywriter objects? Is there any simpler way to do this at all?
2-Since both images could have a value of zero, I could most likely get a division by zero error during division operation. How could I resolve this?
Thanks in advance.
1-Would it make sense to create a 2-dimensional array for each inputfile and read them using binaryreader and binarywriter objects? Is there any simpler way to do this at all?
2-Since both images could have a value of zero, I could most likely get a division by zero error during division operation. How could I resolve this?
Thanks in advance.
•
•
Join Date: Nov 2007
Posts: 37
Reputation:
Solved Threads: 4
It is possible to use a 2 dimensional array. Before declaring the array you should find the length of the file and use that as the size of the array. Then do something like this.
The program will run a little faster if you write the if statements checking for divid by zero in the read loop and write to the file there (in the first set of code at the top of the page).
VB.NET Syntax (Toggle Plain Text)
Dim dblFiles(intFileLen, 1) As Double Dim intI As Integer, dblOut As Double Try 'read the files For intI = 0 To intFileLen - 1 'get the vals & put into the array dblFiles(intI, 0) = streamFile1.ReadInt16 dblFiles(intI, 1) = streamFile2.ReadInt16 Next 'write the vals For intI = 0 To intFileLen - 1 'check the vals for division by 0 If dblFiles(intI, 0) = 0 Or dblFiles(intI, 1) = 0 Then dblOut = 0 Else 'no division error, get the val to write dblOut = dblFiles(intI, 0) / dblFiles(intI, 1) End If 'write the val streamOut.Write(dblOut) Next Catch ex As Exception Throw ex End Try
The program will run a little faster if you write the if statements checking for divid by zero in the read loop and write to the file there (in the first set of code at the top of the page).
Last edited by emurf; Dec 7th, 2007 at 9:58 am.
![]() |
Similar Threads
- input from user from .txt (VB.NET)
- VC++ .net Build problem (C++)
- Mail to a list of people (PHP)
- Vb.net save html to file (ASP.NET)
- setting the web.config file (ASP.NET)
- Java Image Handling (Java)
- Escape and Unescape / Handling (C)
- "cannot resolve symbol"problem (Java)
- CSRSS Backspace Bug in Windows NT 4/NT 2000/NT XP (Windows NT / 2000 / XP)
Other Threads in the VB.NET Forum
- Previous Thread: Closing file in VB.NET
- Next Thread: Send Email with VS 2005 VB.net
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account arithmetic array basic beginner browser button buttons center check code combo component crystalreport cuesent data database datagrid datagridview date datetimepicker design designer dissertation dissertations dissertationtopic dropdownlist excel fade file-dialog filter folder forms ftp generatetags hardcopy html images input insert intel listview monitor net networking number open output panel passingparameters picturebox picturebox1 picturebox2 port print printing problem problemwithinstallation project searchvb.net select serial settings shutdown socket sqlserver survey tcp temperature text textbox timespan toolbox transparency trim txttoxmlconverter updown user usercontol vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic.net visualstudio visualstudio.net visualstudio2008 web winforms wpf wrapingcode year





