2013年8月8日 星期四

C# OpenFileDialog 讀寫文字檔(指定編碼)

讀取
 private string getReadText()
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = ".";
            openFileDialog1.Filter = "文字檔 (*.txt)|*.txt";
            if (openFileDialog1.ShowDialog() != DialogResult.OK) return "";
            StreamReader sr = new StreamReader(openFileDialog1.OpenFile(), System.Text.Encoding.Default);
            return sr.ReadToEnd();
        }
寫入
private void saveFile(string filaPath, string Content)
        {
            FileStream fs2 = File.Open(filaPath, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs2,,System.Text.UnicodeEncoding.UTF8);
            sw.Write(Content);
            sw.Close();
        }

沒有留言:

張貼留言