c# - 'OpenFileDialog' 第一次不取值

标签 c# openfiledialog

选择 .dll 文件并按 OK 后,浏览对话框将关闭并重新打开。在它再次打开并按 OK 后,它会毫无问题地取值。

这是我的代码

private void btnBrowse_Click(object sender, EventArgs e) {
            OpenFileDialog dllDialog = new OpenFileDialog();
            dllDialog.Filter = "DLL Files|*.dll";
            dllDialog.InitialDirectory = @"C:\";
            dllDialog.Title = "Please select .dll file.";
            if (dllDialog.ShowDialog() == DialogResult.OK) {
                dllDialog.ShowDialog();
                tbRepTempLibrary.Text = dllDialog.FileName;

            } else { 
                MessageBox.Show("error");
            }
        }

最佳答案

您正在调用ShowDialog()两次。你应该删除第二个,

private void btnBrowse_Click(object sender, EventArgs e) 
{
    OpenFileDialog dllDialog = new OpenFileDialog();
    dllDialog.Filter = "DLL Files|*.dll";
    dllDialog.InitialDirectory = @"C:\";
    dllDialog.Title = "Please select .dll file.";
    if (dllDialog.ShowDialog() == DialogResult.OK) 
    {
        tbRepTempLibrary.Text = dllDialog.FileName;

    } else 
    { 
        MessageBox.Show("error");
    }
}

关于c# - 'OpenFileDialog' 第一次不取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15944686/

相关文章:

c# - 连接事件处理器

c# - Asp Net MVC Web Api、操作名称或路由

c# - 与参数化插入查询作斗争

c# - 如何将图像文件保存在我的项目文件夹中?

c++ - QFileDialog 将 MIME 类型过滤器组合到 "All formats"

javascript - 可以在 IE 中打开文件输入对话框并上传 onchange 吗?

c# - 返回只读并发列表

c# - 使用 Ninject 启动 MVP WinForms 应用程序

c# - OpenFileDialog 不浏览.NET CF 下的文件夹