Click here to Skip to main content
16,009,068 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
here is code I use to save content of textbox in txt format.
C#
private void button2_Click(object sender, RoutedEventArgs e)
       {
           SaveFileDialog mydialogbox = new SaveFileDialog();
           mydialogbox.DefaultExt = ".txt";
           mydialogbox.Filter = "Text documents (.txt)|*.txt";
           Nullable<bool> result = mydialogbox.ShowDialog();
           if (result == true)
           {
               string filename = mydialogbox.FileName;
              System.IO.File.WriteAllText(filename, textBox1.Text);
           }


        }


Now, I want to allow users to save file also in binary format (beside txt) by selecting option in combobox. Combobox contains 2 values as follows:
1. Save in txt
2. Save in binary
Please, how should I modify my code above in order to make this working.
Thanks.
Posted
Updated 28-Nov-12 1:59am
v2
Comments
[no name] 28-Nov-12 8:00am    
What you have done / researched for binary conversion ?

1 solution

Create a FileStream, attach a BinaryWriter to it, and serialize data using the various BinaryWriter.Write() overloads. Here's an example of the top of my head:
C#
int i = 123;
string s = "Hello";
double d = 1.23456;

using (FileStream fs = new FileStream (@"C:\foo.dat", FileMode.CreateNew)) {
  using (BinaryWriter bw = new BinaryWriter (fs) {
    bw.Write (i);
    bw.Write (s);
    bw.Write (d);
  }
}

/ravi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900