Because the Open and Save inherited from FileDialog same, he made a general method.
// default open path Private String the InitialDirectory = " D: \\ " ; // unified box Private BOOL InitialDialog (the FileDialog fileDialog, String title) { fileDialog.InitialDirectory = the InitialDirectory; // initialize path fileDialog.Filter = " TXT Files ( .txt *, * *) | * .txt;. * *. " ; // filtering options settings, text file, all files. = fileDialog.FilterIndex . 1 ; // currently using the second filter string fileDialog.RestoreDirectory = to true ;// restore the original directory when the dialog is closed fileDialog.Title = title; IF (fileDialog.ShowDialog () == to true ) { for ( int I = . 1 ; I <= fileDialog.FileName.Length; I ++ ) { IF (fileDialog. FileName.Substring (fileDialog.FileName.Length -i, . 1 ) .Equals ( @ " \ " )) { // change the default path to the most recently opened path the InitialDirectory fileDialog.FileName.Substring = ( 0 , fileDialog.FileName.Length - I + 1); return true; } } return true; } else { return false; } }
The open file operation
Private void the Open ( Object obj) { the OpenFileDialog OpenFileDialog = new new the OpenFileDialog (); // open file dialog IF (InitialDialog (OpenFileDialog, " the Open " )) { the using (Stream Stream = openFileDialog.OpenFile ()) { FileName = (( System.IO.FileStream) Stream) .Name; // perform operations related documents
... } } }
Save operation
Private void the Save ( Object obj) { SaveFileDialog SaveFileDialog = new new SaveFileDialog (); // open file dialog IF (InitialDialog (SaveFileDialog, " the Save " )) { the using (Stream Stream = saveFileDialog.OpenFile ()) { FileName = (( System.IO.FileStream) Stream) .Name; // perform a save operation ... MessageBox.Show ( " saved successfully. " ); } } }