关于c#asp.net的问题

时间:2010-05-05 11:52:58

标签: c# asp.net user-controls

为了在asp.net中使用FolderBrowserDialog控件,我不得不在我的项目中添加对System.Windows.Forms的引用。

我写了这段代码:

 FolderBrowserDialog f = new FolderBrowserDialog();  
 f.ShowDialog();  

但是发生了这个错误:

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.  

任何帮助?

2 个答案:

答案 0 :(得分:1)

如果您尝试在ASP.NET应用程序中使用此控件,您将会感到非常失望。

您不能在ASP.NET页面上使用WinForms控件。您应该检查FileUpload控件(它将允许您的用户选择文件并将其上传到网站)。

如果您实际构建的是WinForms应用程序(不是ASP.NET),那么修复非常简单(您应该修复问题和标记):

public static void Main()

变为:

[STAThread]
public static void Main()

请记住,Visual Studio通常会在您创建WinForms项目时将其添加到生成的代码中。

答案 1 :(得分:1)

您无法在ASP.NET中使用Windows窗体控件。事实上,使用ASP.NET中的FolderBrowserDialog没有多大意义,因为它是一个网页。 Web应用程序无法直接访问用户的文件系统。如果要从用户那里获取文件,则应使用FileUpload控件。