Open a text file from another project

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Open a text file from another project

 
0
  #1
May 1st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 6
Reputation: safalmittal is an unknown quantity at this point 
Solved Threads: 0
safalmittal's Avatar
safalmittal safalmittal is offline Offline
Newbie Poster

Re: Open a text file from another project

 
0
  #2
May 2nd, 2007
r u using textwriter or serialize to save to text file. If u could provide some code , it would be better.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: Open a text file from another project

 
0
  #3
May 3rd, 2007
Originally Posted by safalmittal View Post
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 6
Reputation: safalmittal is an unknown quantity at this point 
Solved Threads: 0
safalmittal's Avatar
safalmittal safalmittal is offline Offline
Newbie Poster

Re: Open a text file from another project

 
0
  #4
May 3rd, 2007
Originally Posted by NSta View Post
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: Open a text file from another project

 
0
  #5
May 3rd, 2007
Originally Posted by safalmittal View Post
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 6
Reputation: safalmittal is an unknown quantity at this point 
Solved Threads: 0
safalmittal's Avatar
safalmittal safalmittal is offline Offline
Newbie Poster

Re: Open a text file from another project

 
0
  #6
May 3rd, 2007
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.

Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: Open a text file from another project

 
0
  #7
May 4th, 2007
Originally Posted by safalmittal View Post
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 6
Reputation: safalmittal is an unknown quantity at this point 
Solved Threads: 0
safalmittal's Avatar
safalmittal safalmittal is offline Offline
Newbie Poster

Re: Open a text file from another project

 
0
  #8
May 4th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 21
Reputation: NSta is an unknown quantity at this point 
Solved Threads: 0
NSta NSta is offline Offline
Newbie Poster

Re: Open a text file from another project

 
0
  #9
May 4th, 2007
Originally Posted by safalmittal View Post
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 812
Reputation: arjunsasidharan is on a distinguished road 
Solved Threads: 13
arjunsasidharan's Avatar
arjunsasidharan arjunsasidharan is offline Offline
Practically a Posting Shark

Re: Open a text file from another project

 
0
  #10
May 5th, 2007
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
There is just two ways to live your life.
One is as though nothing is a miracle.
The other is as if everything is.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC