文字コードを指定してテキストをファイルに保存する場合は、System.Text.Encodingクラスを用いてエンコーディングを指定して保存します。
using System.IO;
public void WiteFile(){
string filePath = "c:\data\text.txt";
string text ="AAA BBB\r\n CCC\r\n";
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("shift_jis"));
sw.Write(text);
sw.Close();
}
using System.IO;
public void WiteFile(){
string filePath = "c:\data\text.txt";
string text ="AAA BBB\r\n CCC\r\n";
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("euc-jp"));
sw.Write(text);
sw.Close();
}
using System.IO;
public void WiteFile(){
string filePath = "c:\data\text.txt";
string[] text = GetLines();
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("euc-jp"));
for(int i=0; i<text.Length; i++){
sw.Write(text);
}
sw.Close();
}
using System.IO;
public void WiteFile(){
string filePath = "c:\data\text.txt";
Encoding enc = Encoding.GetEncoding("euc-jp");
string text ="AAA BBB\r\n CCC\r\n";
File.WriteAllText(filePath, text, enc);
/*
//ファイルに追加する場合はAppendAllTextを用いる。
File.AppendAllText(filePath, text, enc);
*/
}