C# openFileDialog筛选器不显示任何选项并显示文件夹中的所有文件(即*.jpeg和*.xlsx)

C# openFileDialog筛选器不显示任何选项并显示文件夹中的所有文件(即*.jpeg和*.xlsx),c#,excel,openfiledialog,C#,Excel,Openfiledialog,我还测试了上面评论的过滤器。 当我浏览时,它没有显示任何筛选器。。。并显示所有文件。我需要的是,当浏览按钮点击,然后只有XLSX或XLS文件显示给用户 提前感谢这是因为您在设置过滤器和过滤器索引之前会显示对话框 您的代码应该是 string file = ""; int size = -1; DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. openFileDialog1.Filter = "Excel

我还测试了上面评论的过滤器。 当我浏览时,它没有显示任何筛选器。。。并显示所有文件。我需要的是,当浏览按钮点击,然后只有XLSX或XLS文件显示给用户


提前感谢

这是因为您在设置过滤器和过滤器索引之前会显示对话框

您的代码应该是

string file = "";
int size = -1;
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
openFileDialog1.Filter = "Excel |*.xlsx"; //"Excel Files|(*.xlsx, *.xls)|*.xlsx;*.xls";
openFileDialog1.FilterIndex = 1;

if(result == DialogResult.OK) // Test result.
{
    file = openFileDialog1.FileName;
    try
    {
        string text = File.ReadAllText(file);
        size = text.Length;
    }
    catch(System.IO.IOException)
    {
    }
}

tempLBL.Text = file;

这是因为在设置过滤器和FilterIndex之前显示对话框

您的代码应该是

string file = "";
int size = -1;
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
openFileDialog1.Filter = "Excel |*.xlsx"; //"Excel Files|(*.xlsx, *.xls)|*.xlsx;*.xls";
openFileDialog1.FilterIndex = 1;

if(result == DialogResult.OK) // Test result.
{
    file = openFileDialog1.FileName;
    try
    {
        string text = File.ReadAllText(file);
        size = text.Length;
    }
    catch(System.IO.IOException)
    {
    }
}

tempLBL.Text = file;

@OP:别忘了把他的帖子标为正确答案@OP:别忘了把他的帖子标为正确答案