在C#中,要设置OpenFileDialog的过滤器,您需要使用OpenFileDialog类的Filter属性。Filter属性是一个字符串,它包含了一个描述和一个扩展名模式,两者之间用管道符(|)分隔。多个过滤器和扩展名模式之间也用管道符分隔。
以下是一个示例,展示了如何设置OpenFileDialog的过滤器:
using System;
using System.Windows.Forms;
namespace OpenFileDialogFilterExample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private void openFileDialogButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            // 设置描述和扩展名模式
            openFileDialog.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
            // 显示OpenFileDialog
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // 获取选中的文件名
                string selectedFileName = openFileDialog.FileName;
                MessageBox.Show("选中的文件: " + selectedFileName);
            }
        }
    }
}
在这个示例中,我们设置了两个过滤器和扩展名模式:
- 文本文件(*.txt)
- 所有文件(.)
当用户打开OpenFileDialog时,将显示这些过滤器。用户可以根据需要选择合适的过滤器。

 便宜VPS测评
便宜VPS测评









