-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.

4

1 回答 1

4

不能同时使用 Windows 和 Web 控件。当OpenFileDialog您的应用程序在localhost. 一旦您将代码发布到服务器,并尝试从客户端运行应用程序,FileDialog将不会在客户端中打开。

如果这是您想要实现的,您可以简单地使用FileUpload控件来上传文件。

于 2015-04-29T21:19:42.680 回答