Save string array to disk?

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
Is it possible to save a sting array to the hard disk and then to appned to the strings over and over?
 
You mean a text file. I know that. If you dont mean a text file please help me understand how else to do it.
 
There is no 4. But I think 3 is what you mean. I wanted to learn how to serialize and deserialize anyway.
 
After staring at it about 10 minutes I finally figured out what they are doing. They are serializing an array of classes. I know that arrays are objects. I should be able to serialize an array, right?
 
To check if something is able to be serialized, you can go to the documentation and see if it has a <Serialiable> attribute on it. Just to be clear, in that example, they are serializing an array of objects in example, not an array of classes. Maybe Im not completely understanding your question, but if you take that example and replace ClassToSerialize with String, you should get your desired effect.
 
It works and saves the file to disk as binary but when I try to deserialize it I get errors.

This is saving it:
Code:
private void SaveB_Click(object sender, System.EventArgs e)
		{
			try
			{
				string[]FormData=GetData();
				string file="CommLog\\"+DateTime.Now.ToLongDateString()+".txt";
				Stream s=new FileStream(file, FileMode.Append, FileAccess.Write);
				BinaryWriter BF=new BinaryWriter(s);
				BF.Write(FormData[0]);
				BF.Write(FormData[1]);
				/*StreamWriter s=new StreamWriter(file,true);
				s.Write(FormData[i]);*/
				s.Close();
				ClearTBs();
			}
			catch{}
		}
		
		private string[]GetData()
		{
			string[]data=new string[20];
			
			data[0]+=this.richTextBox1.Text;
			data[1]+=this.richTextBox2.Text;
			return data;
		}

This is attempting to deserialize:
Code:
this.NewLogB.Enabled=true; NLClosed=0;
					string file="CommLog\\"+DateTime.Now.ToLongDateString()+".txt";
					this.richTextBox1.Clear();
					string[]holdIt=new string[20];
					FileStream FS=new FileStream(file,FileMode.Open);
					BinaryReader BF=new BinaryReader();

//here is where I get the error:::			
holdIt=(string[])BF.Deserialize(FS);
					FS.Close();
					richTextBox2.Clear();
					richTextBox1.Text+=holdIt[0];
					richTextBox2.Text+=holdIt[1];

I cannot figure out where I messed up.

Here is another example modeled after the site:
Code:
this.NewLogB.Enabled=true; NLClosed=0;
					string fil="CommLog\\"+DateTime.Now.ToLongDateString()+".txt";
					File file=new File(fil);
					this.richTextBox1.Clear();
					//StreamReader SR=new StreamReader(file);
					//this.richTextBox1.Text+=SR.ReadToEnd();
					string[]holdIt=new string[20];
					//FileStream FS=new FileStream(file,FileMode.Open);
					Stream FS=file.Open(FileMode.Open);
					BinaryFormatter BF=new BinaryFormatter();
					holdIt=(string[])BF.Deserialize(FS);
					FS.Close();
					richTextBox2.Clear();
					richTextBox1.Text+=holdIt[0];
					richTextBox2.Text+=holdIt[1];

That does not even compile. I get an error about File and also about the arguments in file.Open.
 
Last edited by a moderator:
Back
Top