15

I am working on excel sheets in C# and i am struck to select only excel sheets. I tried the following code

OpenFileDialog browseFile = new OpenFileDialog();
browseFile.DereferenceLinks = true;
browseFile.Filter = "Excel|*.xls|Excel 2010|*.xlsx";
// browseFile.Filter = "Link Files (*.lnk)|*.lnk";

browseFile.Title = "Browse Excel file";
if (browseFile.ShowDialog() == DialogResult.Cancel)

Using this code am not only getting excel sheets but also ended up getting the shortcut files. Can anyone suggest how can i restrict the shortcut files too.

fhnaseer
  • 7,159
  • 16
  • 60
  • 112
G_S
  • 7,068
  • 2
  • 21
  • 51
  • 3
    Counter example from SO http://stackoverflow.com/questions/2350802/can-the-net-openfiledialog-be-setup-to-allow-the-user-to-select-a-lnk-file – Black Maggie Apr 05 '13 at 03:15
  • You might also try combining the filter: `Excel Files|*.xls,*.xlsx` – Sam Axe Apr 05 '13 at 03:21
  • @BlackMaggie I want the link files to be restricted. not to allow them. – G_S Apr 05 '13 at 03:29
  • @G_S: try setting `DereferenceLinks=false;` - I think this is your problem. – code4life Apr 05 '13 at 03:45
  • @code4life I tried setting it to false .Even that didnt help. – G_S Apr 05 '13 at 03:47
  • `OpenFileDialog.DereferenceLinks` if set to true will return the path of the file the shortcut link points to. it is also set to `true` by default. Let me try if there is a way to remove the lnk files altogether. – Vignesh.N Apr 05 '13 at 04:03
  • Yes even i will try out again for a way to remove the lnk files during filtering. – G_S Apr 05 '13 at 04:24

2 Answers2

15

Please see if you are ok with the below approach.
In the meantime let me try if something is possible using reflections.

    OpenFileDialog openKeywordsFileDialog = new OpenFileDialog();
    openKeywordsFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    openKeywordsFileDialog.Multiselect = false;
    openKeywordsFileDialog.ValidateNames = true;
    openKeywordsFileDialog.DereferenceLinks = false; // Will return .lnk in shortcuts.
    openKeywordsFileDialog.Filter = "Excel |*.xlsx";
    openKeywordsFileDialog.FileOk += new System.ComponentModel.CancelEventHandler(OpenKeywordsFileDialog_FileOk);
    var dialogResult =  openKeywordsFileDialog.ShowDialog();


void OpenKeywordsFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
         OpenFileDialog fileDialog = sender as OpenFileDialog;
         string selectedFile = fileDialog.FileName;
         if (string.IsNullOrEmpty(selectedFile) || selectedFile.Contains(".lnk"))
         {
             MessageBox.Show("Please select a valid Excel File");
             e.Cancel = true;
         }
         return;
}
Vignesh.N
  • 2,618
  • 2
  • 25
  • 33
  • You can also consider calling the Win32 API directly http://msdn.microsoft.com/en-us/library/windows/desktop/ms646927(v=vs.85).aspx – Vignesh.N Apr 05 '13 at 04:45
  • How to use this OpenKeywordsFileDialog_FileOk() ? Is it a method or a system generated event. – G_S Apr 05 '13 at 05:00
  • Sorry forgot to add handler for the `FileOk` event. Have edited the answer now. – Vignesh.N Apr 05 '13 at 05:13
  • Yes this worked.But is there any way not to show the .lnk files instead of showing them and restrict them after clicking? – G_S Apr 05 '13 at 05:55
10

Try to use this. Hope this help! Cheers! :D

browseFile.Filter = "Excel files (*.xls or .xlsx)|.xls;*.xlsx";

Ain Ronquillo
  • 197
  • 1
  • 3
  • 6
  • Even this is not working.This ends up showing only shortcuts.I want only excel without shorcuts. – G_S Apr 05 '13 at 03:39
  • Thanks - I needed to know to use the semicolon for separating the extensions (i.e. .xls from .xlsx). – Buck Jan 01 '14 at 01:49