FileDialog.Filter FileDialog.Filter FileDialog.Filter FileDialog.Filter Property

定義

ダイアログ ボックスの [保存先ファイルの種類] または [ファイルの種類] ボックスに表示される選択肢を決定する、現在のファイル名のフィルター文字列を取得または設定します。Gets or sets the current file name filter string, which determines the choices that appear in the "Save as file type" or "Files of type" box in the dialog box.

public:
 property System::String ^ Filter { System::String ^ get(); void set(System::String ^ value); };
public string Filter { get; set; }
member this.Filter : string with get, set
Public Property Filter As String

プロパティ値

ダイアログ ボックスで使用できるファイル フィルター処理オプション。The file filtering options available in the dialog box.

例外

Filter の形式が無効です。Filter format is invalid.

次のコード例ではOpenFileDialog 、のFileDialog実装を使用して、プロパティの作成、設定、およびダイアログボックスの表示について説明します。The following code example uses the OpenFileDialog implementation of FileDialog and illustrates creating, setting of properties, and showing the dialog box. この例ではFilterFilterIndexプロパティとプロパティを使用して、ユーザーのフィルターの一覧を指定します。The example uses the Filter and FilterIndex properties to provide a list of filters for the user. この例では、がButton配置されたフォームSystem.IOと、それに追加された名前空間が必要です。The example requires a form with a Button placed on it and the System.IO namespace added to it.

private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      Stream^ myStream;
      OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

      openFileDialog1->InitialDirectory = "c:\\";
      openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
      openFileDialog1->FilterIndex = 2;
      openFileDialog1->RestoreDirectory = true;

      if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
      {
         if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
         {
            // Insert code to read the stream here.
            myStream->Close();
         }
      }
   }
var fileContent = string.Empty;
var filePath = string.Empty;

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
    openFileDialog.InitialDirectory = "c:\\";
    openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog.FilterIndex = 2;
    openFileDialog.RestoreDirectory = true;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        //Get the path of specified file
        filePath = openFileDialog.FileName;

        //Read the contents of the file into a stream
        var fileStream = openFileDialog.OpenFile();

        using (StreamReader reader = new StreamReader(fileStream))
        {
            fileContent = reader.ReadToEnd();
        }
    }
}

MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim myStream As Stream = Nothing
    Dim openFileDialog1 As New OpenFileDialog()

    openFileDialog1.InitialDirectory = "c:\"
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try
            myStream = openFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Insert code to read the stream here.
            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open.
            If (myStream IsNot Nothing) Then
                myStream.Close()
            End If
        End Try
    End If
End Sub

注釈

フィルター処理の各オプションについて、フィルター文字列には、フィルターの説明と、その後|に縦棒 () とフィルターパターンが含まれています。For each filtering option, the filter string contains a description of the filter, followed by the vertical bar (|) and the filter pattern. 異なるフィルターオプションの文字列は、縦棒で区切られます。The strings for different filtering options are separated by the vertical bar.

フィルター文字列の例を次に示します。The following is an example of a filter string:

Text files (*.txt)|*.txt|All files (*.*)|*.*

複数のフィルターパターンをフィルターに追加するには、ファイルの種類をセミコロンで区切ります。たとえば、次のようにします。You can add several filter patterns to a filter by separating the file types with semicolons, for example:

Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*

FilterIndexプロパティを使用して、ユーザーに対して最初に表示されるフィルター処理オプションを設定します。Use the FilterIndex property to set which filtering option is shown first to the user.

適用対象

こちらもご覧ください