c# - OpenFileDialog is not opening the file -
i have bound openfiledialog control button. on button event, calling openfiledialog control.
now, when running application, openfiledialog box gets open not select file. open button not working.
example code:
private void button1_click(object sender, eventargs e) { openfiledialog1.showdialog(); } private void openfiledialog1_fileok_1(object sender, canceleventargs e) { // logic written here }
it working fine earlier not working.
you need use dialogresult
event of open confirmation user. can use stream read file. here sample code (provided ms in msdn - source:https://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog(v=vs.110).aspx):
private void button1_click(object sender, system.eventargs e) { stream mystream = null; openfiledialog openfiledialog1 = new openfiledialog(); openfiledialog1.initialdirectory = "c:\\" ; openfiledialog1.filter = "txt files (*.txt)|*.txt|all files (*.*)|*.*" ; openfiledialog1.filterindex = 2 ; openfiledialog1.restoredirectory = true ; if(openfiledialog1.showdialog() == dialogresult.ok) { try { if ((mystream = openfiledialog1.openfile()) != null) { using (mystream) { // logic here } } } catch (exception ex) { messagebox.show("error: failed open file. original error: " + ex.message); } } }
Comments
Post a Comment