filtering only excel files in c# -
i working on excel sheets in c# , struck select excel sheets. tried 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 code not getting excel sheets ended getting shortcut files. can suggest how can restrict shortcut files too.
please see if ok below approach.
in meantime let me try if possible using reflections.
openfiledialog openkeywordsfiledialog = new openfiledialog(); openkeywordsfiledialog.initialdirectory = environment.getfolderpath(environment.specialfolder.mydocuments); openkeywordsfiledialog.multiselect = false; openkeywordsfiledialog.validatenames = true; openkeywordsfiledialog.dereferencelinks = false; // 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 openfiledialog; string selectedfile = filedialog.filename; if (string.isnullorempty(selectedfile) || selectedfile.contains(".lnk")) { messagebox.show("please select valid excel file"); e.cancel = true; } return; }
Comments
Post a Comment