| | |
Open a text file from another project
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
okay i have a simple program that has a class of objects some textboxes etc. What i do is enter information in and it saves an object to a text file.
Now i open a new project and want to use the text file that was previously created and read the objects into the new project, am so lost any hlep??
Thanks
Now i open a new project and want to use the text file that was previously created and read the objects into the new project, am so lost any hlep??
Thanks
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
•
•
•
•
r u using textwriter or serialize to save to text file. If u could provide some code , it would be better.
OptionStrictOn Imports System.IO Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Formatters.Soap PublicClass Form1 Inherits System.Windows.Forms.Form Private mFilename As String = "PInfo.txt" Private Sub saveMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveMenuItem.Click 'Save the information Try Dim aBook As New Book Dim informationList As New ArrayList aBook.a = TextBox1.Text aBook.b = TextBox2.Text aBook.c = TextBox3.Text aBook.d = TextBox4.Text aBook.e = TextBox5.Text aBook.f = TextBox6.Text informationList.Add(aBook) Dim infoFS As New FileStream(mFilename, FileMode.Append) Dim infoSF As New SoapFormatter infoSF.Serialize(infoFS, informationList) infoFS.Close() MessageBox.Show("Information Saved") Call clearMenuItem_Click(sender, e) Catch ex As Exception MessageBox.Show("Could not save the file") End Try End Sub Private Sub exitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitMenuItem.Click 'Closes application Me.Close() End Sub Private Sub clearMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearMenuItem.Click 'Clear textboxes TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox5.Text = "" TextBox6.Text = "" TextBox1.Focus() End Sub EndClass
I have been looking for info and am so lost something like i have to do dll, reference my assembly i do not know where to start?? Please any help
Last edited by NSta; May 3rd, 2007 at 2:26 am.
•
•
•
•
OptionStrictOn Imports System.IO Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Formatters.Soap PublicClass Form1 Inherits System.Windows.Forms.Form Private mFilename As String = "PInfo.txt" Private Sub saveMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveMenuItem.Click 'Save the information Try Dim aBook As New Book Dim informationList As New ArrayList aBook.a = TextBox1.Text aBook.b = TextBox2.Text aBook.c = TextBox3.Text aBook.d = TextBox4.Text aBook.e = TextBox5.Text aBook.f = TextBox6.Text informationList.Add(aBook) Dim infoFS As New FileStream(mFilename, FileMode.Append) Dim infoSF As New SoapFormatter infoSF.Serialize(infoFS, informationList) infoFS.Close() MessageBox.Show("Information Saved") Call clearMenuItem_Click(sender, e) Catch ex As Exception MessageBox.Show("Could not save the file") End Try End Sub Private Sub exitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitMenuItem.Click 'Closes application Me.Close() End Sub Private Sub clearMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearMenuItem.Click 'Clear textboxes TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox5.Text = "" TextBox6.Text = "" TextBox1.Focus() End Sub EndClass
I have been looking for info and am so lost something like i have to do dll, reference my assembly i do not know where to start?? Please any help
Hi,
I am assuming Book is some class of yours.
try following code for deserilization in the same/different project .
PrivateSub openMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openMenuItem.Click
Try
Dim aBook As New book
Dim informationList As New ArrayList
Dim infoFS As New FileStream(mFilename, FileMode.Open)
Dim infoSF As New SoapFormatter
'we serialized arraylist, so we will get arraylist object back
informationList = infoSF.Deserialize(infoFS) 'if it doesn't work try informationList = CType(infoSF.Deserialize(infoFS), ArrayList)
infoFS.Close()
'arraylist contained only one item at index=0 and that item was an object of Book type
aBook = CType(informationList(0), book) 'I am assuming book is some class of yours
TextBox1.Text = aBook.a
TextBox2.Text = aBook.b
TextBox3.Text = aBook.c
TextBox4.Text = aBook.d
TextBox5.Text = aBook.e
TextBox6.Text = aBook.f
MessageBox.Show("Information retrieved")
Catch ex As Exception
MessageBox.Show("Could not open the file")
End Try
End Sub
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
•
•
•
•
Hi,
I am assuming Book is some class of yours.
try following code for deserilization in the same/different project .
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim restoredlist As New ArrayList Dim fs As New FileStream(mFilename, FileMode.Open) Dim sf As New SoapFormatter restoredlist = CType(sf.Deserialize(fs), ArrayList) fs.Close() Dim objx As New Book For Each objx In restoredlist informationListBox.Items.Add(objx) Next End Sub
If i place this code in the first project works fine but once i put it in another project it does not, something like i have to reference the assembly but am lost on how to do that? By my understanding i can only reference a dll but i am working with 2003, so am quite lost as i will need to reference the class Book and the text file that is storing the information
Hi,
u have to have the same class in the other project also.
when we serialized arraylist , it had only one item namely a book object.
so what we will get back is ,an arraylist containing one item which will be of book type.
so
objx=ctype(restoredlist(0),book)
should give u that object.
Dim str As New string
For Each str In objx
informationListBox.Items.Add(str)
Next
should work.
u have to have the same class in the other project also.
when we serialized arraylist , it had only one item namely a book object.
so what we will get back is ,an arraylist containing one item which will be of book type.
so
objx=ctype(restoredlist(0),book)
should give u that object.
Dim str As New string
For Each str In objx
informationListBox.Items.Add(str)
Next
should work.
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
•
•
•
•
Hi,
u have to have the same class in the other project also.
when we serialized arraylist , it had only one item namely a book object.
so what we will get back is ,an arraylist containing one item which will be of book type.
so
objx=ctype(restoredlist(0),book)
should give u that object.
Dim str As New string
For Each str In objx
informationListBox.Items.Add(str)
Next
should work.
Additional information: Could not find file "C:\Documents and Settings\owner\My Documents\Visual Studio Projects\Exercise1\bin\PInfo.txt".
So i put the PInfo.txt file in the bin folder and got this error
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in system.runtime.serialization.formatters.soap.dll
Additional information: Parse Error, no assembly associated with Xml key a3:http://schemas.microsoft.com/clr/nsa...eyToken%3Dnull Book.
So i then tried your code and that did not work got the same error as above.
I even tried writing an override toString method in the book class and that did not help, the closest thing i have got to work is commas placed in the listbox.
Last edited by NSta; May 4th, 2007 at 12:56 am.
Hi NSta,
I will try the code myself and tell u if it gets running or aprise u of errors.
meanwhile u can download this 101 solutions from microsoft.
serilization with soap and binary formatters is one of the projects .this thing is a treasure trove of working code.
http://www.microsoft.com/downloads/d...displaylang=en
I will try the code myself and tell u if it gets running or aprise u of errors.
meanwhile u can download this 101 solutions from microsoft.
serilization with soap and binary formatters is one of the projects .this thing is a treasure trove of working code.
http://www.microsoft.com/downloads/d...displaylang=en
Last edited by safalmittal; May 4th, 2007 at 2:54 am.
•
•
Join Date: Nov 2006
Posts: 21
Reputation:
Solved Threads: 0
•
•
•
•
Hi NSta,
I will try the code myself and tell u if it gets running or aprise u of errors.
meanwhile u can download this 101 solutions from microsoft.
serilization with soap and binary formatters is one of the projects .this thing is a treasure trove of working code.
http://www.microsoft.com/downloads/d...displaylang=en
![]() |
Similar Threads
- text file strings help (Visual Basic 4 / 5 / 6)
- read from database and writing the contents into a text file (C)
- How to delete a row in text file? (Visual Basic 4 / 5 / 6)
- Help Reading Info in Text File Into an Array (C++)
- How to send text to a text file using j2me through http connection? (Java)
Other Threads in the VB.NET Forum
- Previous Thread: Timer question: Scoreboard
- Next Thread: how to change the properties of and object using a class module?
| Thread Tools | Search this Thread |
.net 30minutes 2005 2008 access account arithmetic array arrays basic binary bing button buttons c# center check checkbox code combobox component connectionstring convert crystalreport data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dropdownlist excel file-dialog folder ftp generatetags google gridview hardcopy image images inline insert intel internet listview mobile monitor ms net networking output passingparameters peertopeervideostreaming picturebox picturebox1 plugin port print printing problem problemwithinstallation project reports" save searchbox searchvb.net select serial server soap sorting table tcp text textbox timer toolbox trim update updown user usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web wpf





