I want to open a save file dialog, have the user enter a filename, and if they forget the .csv extension, have it tacked on.
It would seem that the SaveFileDialog AddExtension property would work, but its doesn't. I've even set the DefaultExt property to .csv, and still nothing gets tacked on. My file gets saved just fine, but sans extension, so the user can't just double click on the file and have it open in Excel.
I have to be missing something obvious. Here's what I've got
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "*.csv";
sfd.Filter = "Comma Separated(*.csv)|*.*";
if (sfd.ShowDialog() == DialogResult.OK)
{
// Do my file saving
}
Try just using "csv"
for the DefaultExt
- also, you should be using
this (it is IDisposable
):
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.AddExtension = true;
sfd.DefaultExt = "csv";
sfd.Filter = "Comma Separated(*.csv)|*.*";
if (sfd.ShowDialog() == DialogResult.OK)
{
// Do my file saving
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With