Retrieve the selected folder and entered file name from SaveFileDialog

  • Thread starter Thread starter Allen
  • Start date Start date
A

Allen

Hi,

I want to retrieve the selected folder and entered file name from the
SaveFilsDialog. The user is able to create or selected a different
folder during the dialog displayed. If user enters "a/b/c/d" in the
"File name:" box, then the SaveFileDialog.FileName will return
"Path\a\b\c\d" as the name.

Is there any way to just retrieve the selected folder and entered file
name form the SaveFiledialog?

Thanks in advance.

Allen
 
Hi,

You can create a FileInfo object.

FileInfo f = new FileInfo( SaveFileDialog.File);
string directoryName = f.DirectoryName;
string fileName = f.Name;

Hope this helps!
 
Hi,

I don't quite understand your example, but you can use Path.GetDirectoryName
and Path.GetFilename to get them.
 
I want to validate the entered filename. So, I set the SaveFiledialog
ValidateNames property to false and register the event handler for
FileOK.
However, the FileName property returned from SaveFileDialog contains
the full path with the filename. Here is the actual case that I want to
catch:

1.
Selected folder: C:\Temp
File name entered: a\b
FileName is : C:\Temp\a\b

2.
Selected folder: C:\Temp
File name entered: a/b
FileName is : C:\Temp\a/b

Although these inputs will be rejected by the system. Because I set
ValidateNames to false, I won't see the error message from system.
Thus, I would like to get the exact file name entered by the user, so I
can validate the entered text.

Does anyone know how to retrieve the actual text entered by the user
from the SaveFileDialog?

Thanks.
Allen
 
I do not think there is a way to get only the filename from the
SaveFileDialog.
Why don't you set the ValidateNames property to 'true'? Wouldn't this
do exactly what you are trying to do?

If you still would like to validate the filename yourself, you can use
one of the solutions presented above.
If you only want to validate the filename then I you can use the Path
class ( look at the suggestion in a previous post).

If you also want to do read/write operations on the file, then the
FileInfo class may be a good option ( look in the API or my first post).
 
I do not think there is a way to get only the filename from the
SaveFileDialog.
Why don't you set the ValidateNames property to 'true'? Wouldn't this
do exactly what you are trying to do?

If you still would like to validate the filename yourself, you can use
one of the solutions presented above.
If you only want to validate the filename then I you can use the Path
class ( look at the suggestion in a previous post).

If you also want to do read/write operations on the file, then the
FileInfo class may be a good option ( look in the API or my first post).
 
Back
Top