944,051 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 6420
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
May 1st, 2007
0

Open a text file from another project

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
May 2nd, 2007
0

Re: Open a text file from another project

r u using textwriter or serialize to save to text file. If u could provide some code , it would be better.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
safalmittal is offline Offline
6 posts
since May 2006
May 3rd, 2007
0

Re: Open a text file from another project

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
May 3rd, 2007
0

Re: Open a text file from another project

Click to Expand / Collapse  Quote originally posted by NSta ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
safalmittal is offline Offline
6 posts
since May 2006
May 3rd, 2007
0

Re: Open a text file from another project

Hi,
I am assuming Book is some class of yours.
try following code for deserilization in the same/different project .
Yeah i had already set that up in another project which is as follows

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
May 3rd, 2007
0

Re: Open a text file from another project

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.

Reputation Points: 10
Solved Threads: 0
Newbie Poster
safalmittal is offline Offline
6 posts
since May 2006
May 4th, 2007
0

Re: Open a text file from another project

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.
Yeah i had included the Book object (add existing item) but got this error
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
May 4th, 2007
0

Re: Open a text file from another project

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
Last edited by safalmittal; May 4th, 2007 at 2:54 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
safalmittal is offline Offline
6 posts
since May 2006
May 4th, 2007
0

Re: Open a text file from another project

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
Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NSta is offline Offline
21 posts
since Nov 2006
May 5th, 2007
0

Re: Open a text file from another project

why dont you create a class and build it as a DLL then add it up as a component in your other project.. Just a suggestion
Reputation Points: 347
Solved Threads: 13
Practically a Posting Shark
arjunsasidharan is offline Offline
812 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Timer question: Scoreboard
Next Thread in VB.NET Forum Timeline: how to change the properties of and object using a class module?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC