views:

34

answers:

2

in order to use the FolderBrowserDialog control in asp.net, i had to add a reference to System.Windows.Forms in my project.

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?

+1  A: 

If you're trying to use this control in an ASP.NET application, you're going to be sadly disappointed.

You can't use WinForms controls on ASP.NET Pages. You should check out the FileUpload control instead (it will allow your users to pick a file and upload it to the site).

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):

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.

Justin Niessner
well i want the user to locate a path on the server, before he uses FileUpload control...
scatman
There's no built-in control that will do that for you (especially considering exposing your server structure could be considered a huge security risk). I would hightly suggest against it.
Justin Niessner
yeah you are right, but the system is for a private network and only trusted users can access it. i viewed a control done by megapack , it is the same as FolderBrowserDialog (it even asks to add a reference to System.Windows.Forms) but it is not open-source.
scatman
+1  A: 

You cannot use Windows Forms controls in ASP.NET. And in fact using the FolderBrowserDialog from ASP.NET doesn't make a lot of sense, since it is a webpage. Web applications can't get direct access to a user's filesystem. If you want to get a file from the user you should use a FileUpload control.

jkohlhepp
what if it was in the opposite direction. ie i want the user to locate a path on the server?
scatman
Well, you still can't use a Windows Forms control to do that. However, even if you could, it would be a bad idea IMO to expose your server structure to end users across the web. For an intranet application it is *maybe* okay but I would still try to find another way.
jkohlhepp
yes indeed. it is for an intranet application. i already tried a control by megapack, it is the same as FolderBrowserDialog (it even asks to add a reference to System.Windows.Forms) but it is not free
scatman