[C#]C#でTextFieldParserを使う

「TextFieldParser」はVB.NET用に提供されているクラスですが、C#でも使用できます。

TextFieldParserを使用する手順

  1. 参照の追加

    参照設定で「Microsoft.VisualBasic」を追加します。 右クリック画面 参照の追加画面
  2. usingの追加

    「TextFieldParser」を使用するソースファイルの先頭に「using Microsoft.VisualBasic.FileIO;」を追加します。
    using System;
    using System.Windows.Forms;
    using Microsoft.VisualBasic.FileIO;
    
    namespace sampleCSharp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                TextFieldParser textFieldParser = new TextFieldParser(@"c:\test.txt");
            }
        }
    }
    

サンプルコードのダウンロード

サンプルコードの実行には Microsoft Visual Studio 2008 以上のバージョンが必要です。 2008以外のバージョンではプロジェクトを開いた際にファイルの変換が必要な場合があります。その場合は変換後に実行してください。

検証環境

関連ページ