-2

I'm trying to create my first File Dialog box using C# and MS Web Developer 2010. I got the System.Windows.Forms reference set up (from the .NET tab, the one on the COM tab gave me an error), and imported the namespace OK. I found a few very helpful newbie sites for this and I've got the code below.

My problem is that when I invoke it with a button, my browser gives me the 'Waiting for Localhost' message forever and nothing happens. (I did minimize all windows to make sure my dialog box isn't hiding behind something else.) There are a number of posts on forums, including here on StackOverflow (WinFors GUI Hangs When Calling OpenFileDialog.ShowDialog), that discuss this issue, but I can't find a solution.

A lot of forums discuss a resolution that involve making my .NET processes run on different threads. Could it be that Microsoft actually expects me to understand that kind of thing to use this simple control?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void OpenDialog(object sender, EventArgs e)
    {
        String MyFile = string.Empty;
        String input = string.Empty;
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.ShowHelp = true;
        dialog.Filter =  "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        dialog.InitialDirectory = "D:";
        dialog.Title = "This is a test dialog";
        dialog.ShowDialog();
    }
}

EDIT (from comment)

What I'm trying to do is to allow my user to save a file created on the site to a folder of his choosing.

Community
  • 1
  • 1
Stanton
  • 1,334
  • 4
  • 18
  • 32
  • 3
    Are you sure you want web application to open windows forms control on web server? Regarding your question, are you running VS development server or IIS Express? You can check in project properties and IIS should be installed separately. – nrodic Apr 29 '15 at 21:16
  • is this web application hosted in a server? – Saagar Elias Jacky Apr 29 '15 at 21:19
  • Sorry for the delay, and thanks for the response - running VS development server. – Stanton Apr 29 '15 at 22:16
  • What you need is probably [something like this](http://stackoverflow.com/a/21417259/551322). – nrodic Apr 29 '15 at 23:00
  • nrodic, thanks very much. I actually stumbled onto a solution similar to your suggestion, and I'm working with that now. I think my problem is that was trying to trick my application into using windows form functionality, and that's just not going to work. I'll keep baning away at it. Thanks again, amigo - Grateful Noob : | – Stanton Apr 29 '15 at 23:18

1 Answers1

4

You cannot use both Windows and Web controls together. The OpenFileDialog works well when your application is on the localhost. Once you have the code published to the server, and try to run application from client, the FileDialog will not be opened in the client.

You could simply use the FileUpload control for uploading files, if that is what you want to achieve.

Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28
  • Thanks for your response. What I'm trying to do is to allow my user to save a file created on the site to a folder of his choosing. The FileUpload would be an easy way for my user to choose the location, then I could get it from the control and let the rest of my code do the actual work. I found some limitations with SaveAs that I didn't think I could surmount, but I think that may be the only choice I have. – Stanton Apr 29 '15 at 22:18