Skip to content

Instantly share code, notes, and snippets.

@peyangu
Created March 17, 2017 01:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save peyangu/f5f172328230da8b062ffce64c02cf94 to your computer and use it in GitHub Desktop.
TextFieldParserの使用例
string filePath = Console.ReadLine();
string fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1).Replace('"',' ');
// ファイル名と文字エンコードを指定してパーサを実体化
using (TextFieldParser txtParser =
new TextFieldParser(
fileName,
Encoding.GetEncoding("shift_jis")))
{
// 内容は区切り文字形式
txtParser.TextFieldType = FieldType.Delimited;
// デリミタはカンマ
txtParser.SetDelimiters(",");
// ファイルの終わりまで一行ずつ処理
while (!txtParser.EndOfData)
{
// 一行を読み込んで配列に結果を受け取る
splittedResult = txtParser.ReadFields();
using (StreamWriter writer =
      new StreamWriter("sql.txt", true, Encoding.GetEncoding("shift_jis")))
{
// ここでテキストに書き出し
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment