20

I am opening a file using the OpenFileDialog in c# and I am noticing it is taking between 20-40 seconds to load my file and clear the dialog.

Here is my sample code:

private void btnOpen_Click(object sender, EventArgs e)
{
    if (ofdSettings.ShowDialog() == DialogResult.OK)
    {
         // do nothing
    } 
}

even with this limited example it takes the 20-40 second duration for the dialog to clear. the file i'm selecting is a xml file that is only 1.36kb large

svick
  • 236,525
  • 50
  • 385
  • 514
rlemon
  • 17,518
  • 14
  • 92
  • 123
  • normally this is instant. What is the filter on the openfiledialog? – Pinakin Shah Jul 19 '11 at 17:20
  • I have seen this behaviour occur within a windows installation in general (that is, not just when launched from your app). Can you confirm that it works smoothly for other apps too? Possible reason: http://www.pcmag.com/article2/0,2817,1730914,00.asp – Reddog Jul 19 '11 at 17:20
  • 1
    OK, Clarification:: @Pinakin Shah, Filter is as follows `"XML Files(*.XML)|*.xml|All Files (*.*)|*.*"` @Reddog, good call, however I am only seeing the 'delay' after i select a file and click 'open' – rlemon Jul 19 '11 at 17:30
  • @rlemon, try passing the "this" pointer to the ShowDialog and check – Pinakin Shah Jul 19 '11 at 17:33
  • @Pinakin Shah, no such luck. The file still takes a while to load. (if there was a difference, it was negligible) – rlemon Jul 19 '11 at 18:29
  • Use XML deserialization? Use sgen.exe – Hans Passant Jul 20 '11 at 01:00

5 Answers5

31

I had the same problem, openFileDialog1.ShowDialog() was slow, taking 10 seconds after closing it to execute the next line of my program. I noticed in the dialog that I had a couple old shortcuts under "Computer" pointing to webdav url's which were no longer valid. I deleted these shortcuts from windows explorer, and the program is fast now. Check if you have any network connection shortcuts tied to your computer, which also display in the dialog (on the left-hand panel in Windows 7). Try removing them and see if the dialog is faster.

Whitzz
  • 507
  • 4
  • 9
  • Wow! An answer! finally! the project unfortunately has been moved off my plate (for now), But I will be revisiting this one. I will have to keep this in mind (I do know I had networked shortcuts, however I thought all were valid) – rlemon Nov 11 '11 at 19:56
  • Wicked! I had an Ubuntu share in that pane..deleted it and I got a *huge* speed boost! Thanks! – mpen Jun 09 '12 at 21:39
  • Thanks for the hint. For me, removing SMB network share shortcuts from 'this PC' helped. A decade later and MS still sells the same garbage code... – Outfrost Apr 07 '22 at 19:36
11

Another option which helped in my case:

OpenFileDialog ofd = new OpenFileDialog
{
...
   AutoUpgradeEnabled = false
};

With this option, OpenFileDialog renders simpler UI, "pre-Vista" style according to MSDN article.

Stritof
  • 810
  • 7
  • 11
  • 1
    It's unfortunate that you have to essentially downgrade from the newer UI in order to make it usable... But this absolutely worked for me and I'll probably be using this from now on so that I don't have to let the OpenFileDialog load for 30 seconds before I can click anything on it. – Tyler N Aug 02 '19 at 18:07
  • Yes there seems to be a very unfortunate bug in OpenFileDialog. After file selection it hangs. The AutoUpgradeEnabled hack resolves this for .NET 4.8 on Win10 1809 – DAG Apr 06 '21 at 13:57
6

I also had this problem when I want to open a example.url file with file open dialog. It takes 0-10 seconds. Then I find out that this has something todo with the file type association (*.url) When I changed the association from default web browser to notepad++ the problem was gone. But I this was no solution for me, because when somebody clicked on a example.url, the default browser should open this file. To solve this I added DereferenceLinks = false.

OpenFileDialog ofd = new OpenFileDialog
{
...
   DereferenceLinks = false
};

For me this solution works perfect

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
wintlef
  • 123
  • 3
  • 6
0

You can use a free tool like ProcExp (SysInternals.com) to monitor what your application is doing during the lag. Is it scanning the file system? The registry? The network (maybe it is trying to connect to a network share that is slow to respond).

BTW, you can run ProcExp.exe without installing it from http://live.sysinternals.com/!

Philipp Schmid
  • 5,778
  • 5
  • 44
  • 66
  • 1
    I would prefer not to have to download any third party tools. however if no one else has seen this problem and I am left in the dark that may be my only option. Thankyou. – rlemon Jul 19 '11 at 17:31
  • I am giving this the answer, as there is no visible answer I can find here or anywhere else. You provided a good tool for future debug efforts. ***Summary*** *There is no suitable answer as the problem is not reproducible on any other machine.* – rlemon Aug 03 '11 at 19:54
0

This is an old post, but a file browsing dialog still is lacking in WPF. Opening the WinForm OpenFileDialog still presents delay problems when calling multiple times. The solution that worked for me is to not keep the dialog open, but to dispose after finished retrieving the required information, creating a new dialog each time.

            OpenFileDialog fileBrowse = new OpenFileDialog
            {
                Title = MsgBoxCaptions.WinFormUploadFile,
                Filter = string.Format(MPEConstant.JsonFilter_1, MPEConstant.CalFileFormat)
            };

            Forms.DialogResult fileResult = fileBrowse.ShowDialog(parentWindow);
            if (fileResult == Forms.DialogResult.OK)
            {
                calibrationFile = fileBrowse.FileName;
            }
            fileBrowse.Dispose();