-6
namespace SimpleTextEditor
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnOpenFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents (.txt) | *.txt";
            Nullable<bool> result = dlg.ShowDialog();
            if (result==true)
            {
                string filename = dlg.FileName;
                tbEditor.Text = System.IO.File.ReadAllText(filename);
            }
        }

        private void btnSaveFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents (.txt)|*.txt|Binary Files (.bin)|*.bin";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string filename = dlg.FileName;
                System.IO.File.WriteAllText(filename, tbEditor.Text);   
            }

        }
    }
}
Mong Zhu
  • 20,890
  • 7
  • 33
  • 66
  • 5
    You forgot the part where you describe a problem or ask a question. – David Jan 09 '17 at 15:44
  • only code, and no question, this is not a good start. Please take a look on [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Mong Zhu Jan 09 '17 at 15:44
  • Use [WriteAllBytes](https://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes(v=vs.110).aspx) instead of WriteAllText. [This question](http://stackoverflow.com/questions/472906/how-do-i-get-a-consistent-byte-representation-of-strings-in-c-sharp-without-manu) shows how to convert your string to a byte array. – stuartd Jan 09 '17 at 15:46
  • I need to save as text and binary also, – Laurentiu N C Mateescu Jan 09 '17 at 15:49

1 Answers1

2

First create some placeholder for binary extension :

const string BINARY_EXTENSION = "bin";

In btnSaveFile_Click() You can modify the save functionality with:

if ( filename.EndsWith(BINARY_EXTENSION))
    File.WriteAllBytes(filename, Encoding.UTF8.GetBytes(tbEditor.Text)); // Or choose something different then UTF8
else
    File.WriteAllText(filename);

And inside your btnOpenFile_Click you can do the same :

if ( filename.EndsWith(BINARY_EXTENSION))
    tbEditor.Text = Encoding.UTF8.GetString(File.ReadAllBytes(filename); // Or choose something different then UTF8
else
    tbEditor.Text = File.ReadAllText(filename);
Magnetron
  • 4,985
  • 1
  • 16
  • 32
Mateusz
  • 5,613
  • 1
  • 21
  • 29
  • Thanks, that worked !!! – Laurentiu N C Mateescu Jan 09 '17 at 15:59
  • What is the point of having 2 different versions of saving text as UTF8? It simply complicates code for absolutely no reason - just have same code for TXT and mysterious "binary" BIN format. To make answer somewhat reasonable you may want to explain what your "binary" format is and how it is different from the other one. – Alexei Levenkov Jan 09 '17 at 16:11
  • @AlexeiLevenkov That's why i wrote `// Or choose something different then UTF8` which means to choose different method that will "extract" bytes from the text. – Mateusz Jan 10 '17 at 08:09