aewarnick Posted February 11, 2003 Posted February 11, 2003 Is it possible to save a sting array to the hard disk and then to appned to the strings over and over? Quote C#
Moderators Robby Posted February 11, 2003 Moderators Posted February 11, 2003 You can save it to a file. Quote Visit...Bassic Software
aewarnick Posted February 11, 2003 Author Posted February 11, 2003 You mean a text file. I know that. If you don't mean a text file please help me understand how else to do it. Quote C#
Leaders quwiltw Posted February 11, 2003 Leaders Posted February 11, 2003 Serialize the array of strings. A quick search turned up this for some sample code to start from. Look at Example 4. http://www.c-sharpcorner.com/Language/serializingObjectsinCS.asp btw. if this relates to your previous question, I say again: XML, XML , XML Quote --tim
aewarnick Posted February 11, 2003 Author Posted February 11, 2003 There is no 4. But I think 3 is what you mean. I wanted to learn how to serialize and deserialize anyway. Quote C#
Leaders quwiltw Posted February 11, 2003 Leaders Posted February 11, 2003 Ahhh yes 3 is it, not sure where I got 4 from. Quote --tim
aewarnick Posted February 11, 2003 Author Posted February 11, 2003 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? Quote C#
Leaders quwiltw Posted February 12, 2003 Leaders Posted February 12, 2003 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 I'm not completely understanding your question, but if you take that example and replace ClassToSerialize with String, you should get your desired effect. Quote --tim
aewarnick Posted February 12, 2003 Author Posted February 12, 2003 (edited) It works and saves the file to disk as binary but when I try to deserialize it I get errors. This is saving it: 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: 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: 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. Edited February 12, 2003 by aewarnick Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.