6

I need to have an open file dialog for 1000 file types (*.000 - *.999). But adding it to the filter, the dialog gets really slow on choosing file types. Is there anything I can do to speed this up?

string text; 
for (int i = 0; i <= 999; i++)
{
    text.Append("*." + i.ToString("000") + "; ");
}

string textWithoutLastSemicolumn = text.ToString().Substring(0, text.ToString().Length - 2);
dialog.Filter = "Files (" + textWithoutLastSemicolumn + ")|" + textWithoutLastSemicolumn;
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • do you have any network connection shortcuts? – Abhishek Aug 04 '15 at 08:06
  • 1
    May I ask why you would want to do that in the first place? o_O – DerApe Aug 04 '15 at 08:13
  • 1
    I'll join @derape comment, what is the purpose of adding so many extensions to open file dialog? `*.*` is enough. If you are making those files, then you shouldn't be using *different* extensions in first place (you can vary file name instead). It's ok to add few extensions, but not 1000. – Sinatr Aug 04 '15 at 08:30
  • It could be a split zip archive, which uses that naming format (amongst others) to store files. – Saragis Aug 04 '15 at 08:57
  • As @Saragis mentioned, These are a split zip archive. – user2332131 Aug 04 '15 at 10:26

3 Answers3

6

If you're stuck with those extensions, I can see 2 possible solutions. Either accept this rather fast, but not 100% correct solution:

ofd.Filter = "Supported extensions | *.0??;*.1??;*.2??;*.3??;*.4??;*.5??;*.6??;*.7??;*.8??;*.9??";

This will accept all of your extensions, but also values such as .0a1, .99y, and so on. If you know those file extensions will not be a problem in your situation, this might be a good alternative.

A different solution might be to make your own implementation of the OpenFileDialog with support for regular expressions as a filter. This would be the best solution performance and security-wise, but I don't know how hard it'll be.

Saragis
  • 1,782
  • 6
  • 21
  • 30
0

I have tried running the OpenFileDialog and it seems to run fast in my case:

StringBuilder sb = new StringBuilder();
for (int i = 0; i <= 999; i++)
{
    sb.Append("*.");
    sb.Append(i.ToString("000"));
    sb.Append("|");
}
sb.Remove(sb.Length - 1, 1);
string text = sb.ToString();

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Files (" + text + ")|" + text;
ofd.ShowDialog(this);

I have used StringBuilder to build the filter. And then convert it to string and passed it .Filter property.

Just to make sure here is a code that makes 1000-entries in the file types listbox:

StringBuilder sb = new StringBuilder();
for (int i = 0; i <= 999; i++)
{
    sb.Append("*.");
    sb.Append(i.ToString("000"));
    sb.Append("|");
}
sb.Remove(sb.Length - 1, 1);
string text = sb.ToString();

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = text;
ofd.ShowDialog(this);
Joel Legaspi Enriquez
  • 1,236
  • 1
  • 10
  • 14
-2

You can add the following line.

dialog.AutoUpgradeEnabled = false;

This property of the OpenFileDialog will set whether the dialog should upgrade it's behavior and appearance when running on vista or above. By setting it to false we are making the UI rendering simpler which makes the dialog faster.

This the code I tried:

OpenFileDialog dialog = new OpenFileDialog();
StringBuilder text = new StringBuilder();
for (int i = 0; i <= 999; i++)
{
    text.Append("*." + i.ToString("000") + "; ");
}

string textWithoutLastSemicolumn = text.ToString().Substring(0,    text.ToString().Length - 2);
dialog.AutoUpgradeEnabled = false;
dialog.Filter = "Files (" + textWithoutLastSemicolumn + ")|" + textWithoutLastSemicolumn;
dialog.ShowDialog();

I added thsi to Load event of an empty Windows Form

Abhishek
  • 2,925
  • 4
  • 34
  • 59