[C#] How to save text from the multiline TextBox2 as Unicode Encoding?

Discussion in 'Mixed Languages' started by hajra, Dec 26, 2014.

  1. hajra

    hajra MDL Novice

    Nov 5, 2013
    1
    0
    0
    #1 hajra, Dec 26, 2014
    Last edited by a moderator: Apr 20, 2017
    Hello everyone.
    This is my code for saveFileAs and saveFile for multiline TextBox2, and it works well.
    When I save the text in TextBox2, to be saved as UTF-8 course. (example .txt, Encoding UTF-8).


    But I want it to be saved as Unicode (UTF-16).
    What needs to change or add in my code, that be text saved as Unicode (UTF-16), for example (.txt, Encoding Unicode)?
    Please help me. Thank you.




    Code:
    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
    
                saveFileAs();
            }
            private void saveFileAs()
            {
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Title = "Save file( .txt, .srt...)";
                saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.RestoreDirectory = true;
                //Set default extension as .txt
                saveFileDialog1.DefaultExt = ".txt";
    
    
    
    
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
    
    
                    currentFile = saveFileDialog1.FileName;
                    this.Text = currentFile + " File";
                    saveFile();
    
    
    
    
                }
                toolStripButton8.Enabled = true;
                saveToolStripMenuItem.Enabled = true;
                toolStripButton12.Enabled = true;
                printToolStripMenuItem.Enabled = true;
                toolStripButton11.Enabled = true;
                printpreviewToolStripMenuItem.Enabled = true;
    }
    private void saveToolStripMenuItem_Click_1(object sender, EventArgs e)
            {
                saveFile();
            }
    private void saveFile()
            {
                
                System.IO.StreamWriter sw = new System.IO.StreamWriter(currentFile);
                sw.Write(textBox2.Text);
                sw.Close();
                MessageBox.Show("saved!");
    
    
            }
    
     
  2. Kasedo

    Kasedo MDL Novice

    Jul 19, 2008
    4
    0
    0
    Try this

    System.Text.Encoding.Unicode.GetString( System.Text.Encoding.Convert(System.Text.Encoding.UTF8,System.Text.Encoding.Unicode,System.Text.Encoding.UTF8.GetBytes(textBox2.text)));
     
  3. Inge

    Inge MDL Member

    Apr 2, 2008
    188
    479
    10
    #3 Inge, Jan 3, 2015
    Last edited by a moderator: Apr 20, 2017
    Code:
    System.IO.File.WriteAllText("Path", "content", System.Text.Encoding.Unicode);