OpenFileDialog/c# slow on any file. better solution?

倖福魔咒の 提交于 2020-01-10 01:05:08

问题


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


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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/!



来源:https://stackoverflow.com/questions/6751188/openfiledialog-c-slow-on-any-file-better-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!