跳到主要內容

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();
        }

留言

這個網誌中的熱門文章

java西元民國轉換_各種不同格式

C#資料庫操作(新增、修改、刪除、查詢)