Thursday, June 4, 2009

openFileDialog problem to open shortcuts and a solution

I found a problem with SSMPropEditor that when I select a shortcut to a file with the open dialog box it tried to open the actual shortcut and not the actual file it pointed to that it should follow its way to.

DereferenceLinks property is used to get or set a value indicating whether the dialog box returns the location of the file referenced by the shortcut or whether it returns the location of the shortcut (.lnk). But the default is true and still it did not work to dereference the link.

I also found that it worked on XP but not on Vista and Windows 7. It didn’t matter if I changed the .NET Framework version from 2.0 to 3.5 either.

Here is the code that returns the LNK file instead of the TXT file.

openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FileName = "*.txt";
openFileDialog1.DefaultExt = "txt";
openFileDialog1.FilterIndex = 1;
DialogResult res = openFileDialog1.ShowDialog();

The solution that I found after some trial and error was the following. Notice the FileName property change. I made sure to not use the asterisk.

openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FileName = "";
openFileDialog1.DefaultExt = "txt";
openFileDialog1.FilterIndex = 1;
DialogResult res = openFileDialog1.ShowDialog();


Some of the latest blog posts

Subscribe to RSS headline updates from:
Powered by FeedBurner