简体   繁体   English

关于C#asp.net的问题

[英]question about c# asp.net

in order to use the FolderBrowserDialog control in asp.net, i had to add a reference to System.Windows.Forms in my project. 为了在asp.net中使用FolderBrowserDialog控件,我必须在项目中添加对System.Windows.Forms的引用。

i wrote this code: 我写了这段代码:

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

but this error occured: 但发生此错误:

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.  

any help? 任何帮助?

If you're trying to use this control in an ASP.NET application, you're going to be sadly disappointed. 如果您试图在ASP.NET应用程序中使用此控件,您将很失望。

You can't use WinForms controls on ASP.NET Pages. 您不能在ASP.NET页上使用WinForms控件。 You should check out the FileUpload control instead (it will allow your users to pick a file and upload it to the site). 您应该改为检出FileUpload控件(它将允许您的用户选择一个文件并将其上传到站点)。

If you're actually building a WinForms application (not ASP.NET) , then the fix is quite easy (and you should fix your question and tagging): 如果您实际上是在构建WinForms应用程序(而不是ASP.NET) ,则此修复非常简单(并且您应该修复问题和标记):

public static void Main()

Becomes: 变为:

[STAThread]
public static void Main()

Keep in mind though, Visual Studio usually adds this to your generated code when you create a WinForms project. 但是请记住,Visual Studio通常在创建WinForms项目时将其添加到生成的代码中。

You cannot use Windows Forms controls in ASP.NET. 您不能在ASP.NET中使用Windows窗体控件。 And in fact using the FolderBrowserDialog from ASP.NET doesn't make a lot of sense, since it is a webpage. 实际上,从ASP.NET使用FolderBrowserDialog并没有多大意义,因为它是一个网页。 Web applications can't get direct access to a user's filesystem. Web应用程序无法直接访问用户的文件系统。 If you want to get a file from the user you should use a FileUpload control. 如果要从用户获取文件,则应使用FileUpload控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM