1

Pretty new to c# and MVC. I've got a MVC application setup in which a webapp runs under a chrome like browser control. I have button inside the webapp (html) from which user able to select folders he wants, then I want the actual path of them.

For that I am making an ajax request to my MVC controller which will open folderBrowserDialog (System.Windows.Forms) then returning path back as the ajax response. Everything works fine when I run the application with visual studio. But after making the package of the application and installing the exe, the folderBrowserDialog isn't appearing at all. There are no errors thrown the ajax respons, properly with a value null

here is the code (part of it )

selectFolderGlobal is a global variable

public JObject OpenFolderExplorer()
    {
        try
        {
            Thread fb= new Thread(new ThreadStart(openFileBrowser), 1);
            fb.SetApartmentState(ApartmentState.STA);
            fb.Start();
            fb.Join();
            JObject selectedFolder = new JObject();
            selectedFolder.Add("selectedFolder", selectedFolderGlobal);
            return selectedFolder;
        }
        catch (Exception ex)
        {
            Logger.Log(" Exception: " + ex.Message);
            JObject errorcode = JObject.Parse(mConstants.EXCEPTION);
            return errorcode;
        }
    }


private void openFileBrowser()
    {
        try
        {
            var fbd= new FolderBrowserDialog();
            fbd.ShowNewFolderButton = false;
            DialogResult result = fbd.ShowDialog(new Form() { TopMost = true, WindowState = FormWindowState.Minimized });
            if (result == DialogResult.OK)
            {
                selectedFolderGlobal= fbd.SelectedPath;
            }
        }
        catch (Exception ex)
        {
            Logger.Entry(" Exception: " + ex.Message);
        }
    }

Ajax response comes back like this

{
  "selectedFolder":null
}

Anybody has a clue why it might be happening only after the package creation ( after making an .exe out of it) ? The System.Windows.Forms.dll is added to the dependancy ( if it wasn't it should've thrown an exeption or even the package build would've been failed)

P-RAD
  • 1,293
  • 2
  • 15
  • 36
  • Just debugged it with locally hosted IIS got an exception leads to this SO link:https://stackoverflow.com/questions/8928713/how-to-resolve-error-showing-a-modal-dialog-box-or-form-when-the-application-i ("Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.") – P-RAD Nov 29 '17 at 04:58
  • Searching with the above message gave an idea that it might not be possible, but in a case where everything ( client, server ) all in the same machine , Couldn't really figure out is there a workaround or not . "ServiceNotification or DefaultDesktopOnly style to display a notification from a service application" - there might be a way to do that – P-RAD Nov 29 '17 at 23:46

1 Answers1

1

You are trying to open browse folder dialog on web server, user browser is running on his local machine, even if this works user cannot see dialog that you raised on server these are separate machines. And web application has the file usage rights only under web application root directory, you cannot use other folders, and really you don't need it.

You have to understand how web applications work, all c# code is executed on server, and that c# code produces html, css, javascript and other files, then browser downloads that content and use it on local machine. Browser renders html, executes javascript...

When you are debugging on local host then your computer is both server and client, that is the reason this working in VS and you are probably runnin it as administrator on local machine.

Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • Thank you for that. The whole web app runs inside a windows form in an attached web browser control I was able to accomplish same via using the form which JavaScript was able to communicate to using registerjsObject method – P-RAD Dec 01 '17 at 03:08