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!"); }
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)));