OpenFileDialog中的异常 [英] Exception in OpenFileDialog

查看:69
本文介绍了OpenFileDialog中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好会员,
我在使用OpenFileDialog时遇到异常,以下是代码

hello members,
i m getting an exception while working with OpenFileDialog,following is the code

private void btnBrowse_Click(object sender, EventArgs e)
       {
           OpenFileDialog f = new OpenFileDialog();
           if(f.ShowDialog()==DialogResult.OK)
           {
               txtDoc.Text = f.FileName;
           }
       }




异常消息是:

必须先将当前线程设置为单线程单元(STA)模式,然后才能进行OLE调用.确保您的Main函数上已标记STAThreadAttribute.仅当将调试器附加到进程时,才会引发此异常.


请帮助我...为什么我要得到这个消息...我已经包含了所有必需的名称空间...




and the exception message is:

Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.


plz help me...why m i getting this msg...i have included all the necessary namespaces...

推荐答案



需要在单线程(STA)中运行OLE调用(如OpenFileDialog),您的线程正在多线程(MTA)中运行.

只需将[STAThread]添加到您的main()方法中.

问候
Hi,

it''s needed to run OLE calls (like the OpenFileDialog) in a Single Thread (STA), your thread is running in Multithread (MTA).

Just add [STAThread] to your main() method.

Regards


这篇关于OpenFileDialog中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆