Read a text file from another project

bbelect

Member
Joined
Apr 20, 2007
Messages
14
Programming Experience
1-3
okay i have a simple program which allows a user to 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

Original Project

VB.NET:
[SIZE=2][COLOR=#0000ff]Option[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Strict[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]On[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.IO[/SIZE]
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Runtime.Serialization[/SIZE]
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Runtime.Serialization.Formatters.Binary[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1[/SIZE]
[SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.Form[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] mFilename [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "PInfo.txt"[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] saveMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] saveMenuItem.Click[/SIZE]
[SIZE=2][COLOR=#008000]'Save the information[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ablackBook [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] BlackBook[/SIZE]
[SIZE=2]ablackBook.Name = nameTextBox.Text[/SIZE]
[SIZE=2]ablackBook.PhoneNumber = phoneTextBox.Text[/SIZE]
[SIZE=2]ablackBook.PagerNumber = pagerTextBox.Text[/SIZE]
[SIZE=2]ablackBook.CellNumber = cellTextBox.Text[/SIZE]
[SIZE=2]ablackBook.VoiceNumber = voiceTextBox.Text[/SIZE]
[SIZE=2]ablackBook.Email = emailTextBox.Text[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] personalInformationFileStream [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(mFilename, FileMode.Append)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] personalInformationBinaryFormatter [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] BinaryFormatter[/SIZE]
[SIZE=2]personalInformationBinaryFormatter.Serialize(personalInformationFileStream, ablackBook)[/SIZE]
[SIZE=2]personalInformationFileStream.Close()[/SIZE]
[SIZE=2]MessageBox.Show("Information Saved")[/SIZE]
[SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2] clearMenuItem_Click(sender, e)[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]MessageBox.Show("Could not save the file")[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Last edited:
Then i do this in another project, and place the text file in the bin folder of this project

VB.NET:
[SIZE=2][COLOR=#0000ff]
Option[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Strict[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]On
Imports[/COLOR][/SIZE][SIZE=2] System.IO
[/SIZE][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Runtime.Serialization
[/SIZE][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Runtime.Serialization.Formatters.Binary
 
[/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1
[/SIZE][SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.Form
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] mFilename [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "PInfo.txt"[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] BBFS [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] FileStream
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] BBBF [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] BinaryFormatter
informationListBox.Items.Clear()
BBFS = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(mFilename, FileMode.Open)
[/SIZE][SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] (BBFS.Position < BBFS.Length)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objBB [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] BlackBook
objBB = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](BBBF.Deserialize(BBFS), BlackBook)
informationListBox.Items.Add(objBB.Name)
[/SIZE][SIZE=2][COLOR=#0000ff]Loop
[/COLOR][/SIZE][SIZE=2]BBFS.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/SIZE]

but for this code i get an error
objBB = CType(BBBF.Deserialize(BBFS), BlackBook)

Additional information: Cannot find the assembly Work, Version=1.0.2678.17853, Culture=neutral, PublicKeyToken=null.
 
add a referrence to the Work assembly that created this serialized class

(i.e. you cannot serialize an object into a file and then deserialize it elsewhere that has no informatio nabout it..

Or put another way: you cannot encode a DVD into DivX video on your home pc, and then expect it to play on your work PC that doesnt have the DivX codec installed"
 
Thanks i was totally lost but i do have a question. DO i have to create a dll file i found a tutorial which states i must Change the OutputType = "WinExe" to OutputType = "Library", and the StartupObject = "sample.Form1" to StartupObject = "". in the vbproj extension. I do all of that and it creates the dll file. Now i get lost maybe somebody cn point me in the direction i need to go from the beginning so i fully understand i dont just want to put someone elses code in my work
 
What's the question, then? Your post contains no question marks!

SOrry bout that. My question is how do i reference the assembly? because i have the text file and class set up in another project i want to use them in a new project, so from a previous post "reference the work assembly". When i tried to find out more online a bit of a run around creating dll etc.
 
OK, well.. i'm not sure which part of the process you are meaning so here's a full run down:

I would:
Open the IDE
New Project
Class Library template
Rename Class1 to Say, make a function called Hello
Change the namespace to Messager and save the project
Build it into a DLL, it will be called Messager.dll

Open up another IDE
New Project
Windows Forms app
Click the Show All Files toolbar button in Solution Explorer
Right click References (there are other ways of doing this, like project menu, or right click the project in Soln Expl and choose Add Reference) and choose Add
I choose Browse
I pick the DLL
In my Form1, i write:
VB.NET:
Dim x as New Messager.Say
x.Hello()
The message would appear
 
I have vs2003 so i do think i can do a dll? I have been looking around and found this is this correct and if possible how could i do it, thanks.


A type is considered to be part of the assembly it is defined in. Onesolution is to add a reference to App1 to the project that created App2 anddeploy App1 along with App2. Another solution is to redefine the objectdefinition in App2, and then implement a serialization binder to map thetype as defined in App1 to a type defined in App2.To do this implement a serialization binder. During deserialization thisgives you a chance totell the binary formatter which System.Type to use for a given typename. Youneed to define a class that derives from SeriazationBinder and in that classoverride the BindToType method. When you create the BinaryFormatter objectset its Binder property to an instance of your SerializationBinder class. Asthe binary formatter deserializes if it hits a typename for which it doesnot already have a System.Type defined for it, it asks the binder for thatSystem.Type. This gives you a chance to treat a type as defined in oneassembly toa type as defined in some other assembly. It is up to you to make sure thetypes reallyare compatible and convertible.
 
I dont think 2003 will work significantly differently for soemthing as basic as this. How far have you got?
 
Back
Top