关于c#asp.net的问题

关于c#asp.net的问题,c#,asp.net,user-controls,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

为了在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.  

有什么帮助吗?

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

不能在ASP.NET页面上使用WinForms控件。您应该签出该控件(它将允许您的用户选择一个文件并将其上载到站点)

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

变成:

[STAThread]
public static void Main()

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

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

在asp.net应用程序中不应使用WinForms控件。可以向我们展示更多您正在做的事情。我想让系统管理员找到用户文件应该保存在(服务器上)的路径。如果是相反的方向,该怎么办。ie我想让用户在服务器上找到一个路径?好吧,你仍然不能使用Windows窗体控件来实现这一点。但是,即使可以,在IMO中,将服务器结构在web上向最终用户公开也是一个坏主意。对于内联网应用程序来说,这也许是可以的,但我仍然会尝试找到另一种方法。它适用于内部网应用程序。我已经尝试了megapack的一个控件,它与FolderBrowserDialog相同(它甚至要求添加对System.Windows.Forms的引用),但它不是freewell,我希望用户在使用FileUpload控件之前在服务器上找到一个路径……没有内置控件可以为您这样做(特别是考虑到暴露您的服务器结构可能会被认为是一个巨大的安全风险)。我强烈建议您不要这样做。是的,您是对的,但该系统是专用网络,只有受信任的用户才能访问它。我查看了megapack完成的控件,它与FolderBrowserDialog相同(它甚至要求添加对System.Windows.Forms的引用),但它不是开源的。
[STAThread]
public static void Main()