Openfiledialog 在 windows 10 上运行良好,但在 windows 7 上冻结 windows 应用程序

Openfiledialog works fine on windows 10, but freezes windows app on windows 7

我有windows应用程序,我的程序有以下代码:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}

现在,在 MainForm() 上,我有几个按钮,单击每个按钮后,我隐藏 MainForm 并使用 opendialog 打开一个新表单(Windows 表单),如下所示代码:

this.Hide();
TestCenter testCenter = new TestCenter();
testCenter.ShowDialog();
this.Show();

现在,在 TestCenter 窗体中,我有一个用于选择文件的功能 (OpenFileDialog),如下面的代码所示:

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "image file |*.jpg;*.png;*.gif";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.Cancel)
    return;
pictureBox_PartImage.Image = Image.FromFile(ofd.FileName);
txt_ImagePath.Text = ofd.FileName;

我有一个 TextBox 和一个 PictureBox,用于在 OpenFileDialog 中进行选择后显示文件路径和图像。

奇怪的是,当我从 Visual Studio 或笔记本电脑 (Windows 10) 上安装的程序 运行 这个程序时,它运行良好,没有任何问题. 但是,当我在客户端计算机 (Windows 7) 上安装它时,当我单击调用此 OpenFileDialog() 的按钮时,它会冻结此 Windows 表单应用程序。

有人可以帮我解决这个问题吗?

--------EDIT--------2/7/18--------

private void btnImage_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    // ofd.ShowHelp = true;
    ofd.Filter = "Image Files (*.png, *.gif, *.jpg)|*.png; *.gif*;*.jpg";
    DialogResult dr = ofd.ShowDialog();
    if (dr == DialogResult.Cancel)
        return;
    pictureBox_PartImage.Image = Image.FromFile(ofd.FileName);
    txt_ImagePath.Text = ofd.FileName;
}

我不认为你的代码有问题。我怀疑是安装问题。确保此程序首先运行:

using System;
using System.Windows.Forms;

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.ShowDialog();
    }
}

确保您已安装目标 .NET 运行时的正确版本。

OpenFileDialog 会将 shell 扩展加载到您的应用程序中,如果客户端计算机安装了奇怪的 shell 扩展,那么它可能会干扰您的应用程序。

在 Windows 7 机器上调试程序以缩小问题范围。

  • 使用调试符号编译程序
  • 在 Windows 7 机器上安装 remote debugging
  • 在 Windows 7 机器上启动您的程序
  • 在您的开发者机器上使用 Visual Studio 连接到 Windows 7 台机器
  • 在按钮上放置一个断点以暂停代码
  • 交互式调试以查看究竟是什么调用挂起

    进一步排除故障,您将获得更多信息。我之前在为 ANYCPU 构建时已经看到过这个。尝试 x86 进行测试。

显然,将 OLE DB Services = -1 添加到我的连接字符串中解决了这个问题。

在我的表单中,我使用 Access DB Connection 从数据库中获取数据。这是搞乱加载 OpenFileDialog 的那个。而且,这也解释了为什么示例代码(由 Wyck 编写)运行良好(因为其中没有使用 DB Connection)。

我想知道为什么 Vikas4u 对 this 问题(这是我的参考)的回答被否决了。