C#でテキストファイルを読み込むサンプルです。
using System.IO;
string str;
try
{
using (StreamReader sr = new StreamReader("test.txt", Encoding.GetEncoding("Shift_JIS")))
{
for (; ; )
{
str = sr.ReadLine();
if (str == null)
{
break;
}
// 読み込んだ1行分の文字列に対する任意の処理
}
}
}
catch (Exception ex)
{
// ファイルがない場合、他例外発生の場合
}
[0回]
PR