gpt4 book ai didi

c# - 打开文件对话框错误?

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:16 24 4
gpt4 key购买 nike

这是我点击保存按钮时的代码:

OpenFileDialog myOpenfileDialog = new OpenFileDialog(); 
myOpenfileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
myOpenfileDialog.FilterIndex = 2;
myOpenfileDialog.RestoreDirectory = true;

if (myOpenfileDialog.ShowDialog() == DialogResult.OK)
{
FileUploadData.SaveAs(myOpenfileDialog.FileName);//my file upload control taking the path to save the file to the selected location
}

我遇到了这个异常:

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. this is a threadStateException

最佳答案

好吧,这个消息对我来说似乎相当清楚——您需要使用 STA 线程。如果您正在编写一个 Windows 窗体应用程序,您可以通过确保您的 Main 方法装饰有如下属性来实现此目的:

[STAThread]
static void Main(string[] args)
{
...
}

如果您正在编写一个 ASP.NET 应用程序,您不应该首先尝试在您的代码中使用 Windows 窗体控件。我提到这个是因为你用 both winformsasp.net 标记了你的问题 - 你不应该混合这两者的代码.

关于c# - 打开文件对话框错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4182349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com