OpenfileDialog - 窗口未弹出

发布于 2024-09-26 12:02:45 字数 539 浏览 5 评论 0原文

我正在研究表格。我希望当我单击按钮时弹出小窗口,并从各个文件夹中选择我选择的 XML 文件。

我想,这个 OpenFileDialog 会对我有帮助。

private void button3_Click(object sender, EventArgs e)
{
   /
    OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

    openFileDialog1.Filter = " XML Files|*.xml";

    openFileDialog1.InitialDirectory = @"D:\";



    if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
    {
        MessageBox.Show(filed.FileName.ToString());
    }
}

我尝试使用以下代码,但是当我单击按钮时,不会弹出窗口。 我不明白我犯了什么错误。

这有什么问题吗?

谢谢!

I am working on form . I want small window to pop up when I click button and to will select XML file of my choice from various folder.

I guess, this OpenFileDialog will help me.

private void button3_Click(object sender, EventArgs e)
{
   /
    OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

    openFileDialog1.Filter = " XML Files|*.xml";

    openFileDialog1.InitialDirectory = @"D:\";



    if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
    {
        MessageBox.Show(filed.FileName.ToString());
    }
}

I tried using following code but when I click on the button there window doesn't pop up.
I am not geting what mistake I have made.

What is the problem with that?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-10-03 12:02:45

您不能只是打开 来自控制台应用程序的文件对话框。您必须通过一些设置来解决这个问题 single线程公寓 (STA)。

[STAThread]
static void Main(string[] args)
{
            MessageBox.Show("Test");
}

--编辑--

点击事件的以下工作:

OpenFileDialog f = new OpenFileDialog();
f.Filter = "XML Files|*.xml";
f.InitialDirectory = "D:\\"; 
if(f.ShowDialog() == DialogResult.OK)
{
    MessageBox.Show(f.FileName);  
}

You cant just open the file dialog from a console app. You will have to workaround it with some setting to single thread apartment (STA).

[STAThread]
static void Main(string[] args)
{
            MessageBox.Show("Test");
}

--EDIT--

Following works on click event:

OpenFileDialog f = new OpenFileDialog();
f.Filter = "XML Files|*.xml";
f.InitialDirectory = "D:\\"; 
if(f.ShowDialog() == DialogResult.OK)
{
    MessageBox.Show(f.FileName);  
}
走过海棠暮 2024-10-03 12:02:45

您无法在控制台应用程序中打开文件日志。

你说我有按钮,所以这一定是Win应用程序,使用

openFileDialog1.ShowDialog();失踪了

private void button3_Click(object sender, EventArgs e)
        {
           OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter = " XML Files|*.xml";

            openFileDialog1.InitialDirectory = @"D:\";

            openFileDialog1.ShowDialog();

            // Get file name and use OpenFileDialog1.FileName or something like that

       }

You cant open file fialog in console app.

You say I have button, so this must be Win app, use

openFileDialog1.ShowDialog(); is missing

private void button3_Click(object sender, EventArgs e)
        {
           OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter = " XML Files|*.xml";

            openFileDialog1.InitialDirectory = @"D:\";

            openFileDialog1.ShowDialog();

            // Get file name and use OpenFileDialog1.FileName or something like that

       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文