OpenFileDialog is disposable (so use "using"), and no real purpose in the
variable for the dialog result (since only used once in the very next line),
but otherwise looks OK; my suggestion:
string fileName = "";
using (OpenFileDialog ofd = new OpenFileDialog()) {
if (ofd.ShowDialog() == DialogResult.OK) {
fileName = ofd.FileName;
}
return fileName;
}
You could also possibly "return ofd.FileName" (and return "" or return null
at the bottom), but then you get into holy wars of people saying "one
method, one return". Personally in a short snippet like this it wouldn't
bother me as every path is still very clear; I only mention it for
completeness.
=?iso-8859-1?Q?Re:_open_file_dialogue_+_Fehlerbehandlung_bei_falsch_gew?= =?iso-8859- ... Klickst Du auf 'Ok' bei ausgewähltem Dateinamen, so kannste dann per 'string filename=openFileDialog.FileName;' den Dateinamen der ausgewählten Datei auslesen. ... Klickst Du auf 'Cancel' so bleibt die Variable 'FileName' des OpenFileDialogs's auch bei ausgewählten Dateinamen leer. ... Sobald Du also auf 'OK' oder 'Cancel' geklickt hast schließt sich der OpenFileDialog unweigerlich.... (microsoft.public.de.german.entwickler.dotnet.csharp)
Re: openfiledialog control locks folder ... actual path, filename and extension of the file, so i no longer need the ...openfiledialog, thus i am using openfiledialog1.resetmethod to reset it. ... The problem i am encountering is that the folder in which the selected file ... (microsoft.public.dotnet.languages.vb)
RE: OpenFileDialog and SaveFileDialog ... Neither OpenFileDialog, nor SaveFileDialog actually open a file. ... Here we call the OpenFileDialog and select a filename to be opened and then ... button is selected we call the OpenFile method....private void OnFileOpen ... (microsoft.public.dotnet.languages.csharp)
Re: openFileDialog ...filename before the dialog has closed. ... that launches your other form. ... get whatever data you need from the OpenFileDialog... > being closed to open a modal form.... (microsoft.public.dotnet.languages.csharp)
Re: openFileDialog ...filename before the dialog has closed. ... that launches your other form. ... get whatever data you need from the OpenFileDialog... > being closed to open a modal form.... (microsoft.public.dotnet.framework.windowsforms)