我执行下面的代码产生了错误: OpenFileDialog fdlg = new OpenFileDialog();
if (fdlg.ShowDialog() == DialogResult.OK)// 这里错了??
{
filename = fdlg.FileName;//FileName是全局变量
}在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常。
if (fdlg.ShowDialog() == DialogResult.OK)// 这里错了??
{
filename = fdlg.FileName;//FileName是全局变量
}在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常。
Thread app = new Thread(new ParameterizedThreadStart(ShowWindow)); app.ApartmentState = ApartmentState.STA;
或Thread newThread = new Thread(new ThreadStart(ThreadMethod)); newThread.SetApartmentState(ApartmentState.MTA);
protected void Button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(Test);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
Protected void Test()
{
SaveFileDialog sd = new SaveFileDialog();
sd.FileName = "adfdsad";
sd.DefaultExt = "Txt files (*.txt)|*.txt";
sd.ShowDialog();
}
在exe中,这样:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
http://topic.csdn.net/u/20100105/18/036abe84-bacb-4537-beff-17f40ce9f122.html只不过一个是打开对话框,一个是保存对话框