smithvoice.com
 Y'herd thisun? 

“! could either watch it happen, or be part of it.”
-Elon Musk



Saving extended extensions with the FileSaveDialog

TaggedCoding, CSharp

Neat how MS breaks the old rules of 8.3 with all of the dlls of the Framework, isn't it? The file names started out as direct representations of namespace hierarchies but as with all good intentions that didn't last long; We did however get the thumbs-up on it being a big-company standard to do XXX.XXX.XXX.XXX....XXX files naming to make the files themselves more intutitive.

Little sticky point is how to get the FileSaveDialog to save the darned extended extensions.

If you just...

using (SaveFileDialog fo = new SaveFileDialog())
{
fo.Filter = "PurchaseOrder xml (*.po.xml)|*.po.xml|xml|*.xml";
fo.FilterIndex = 0;
fo.OverwritePrompt = true;
if (fo.ShowDialog() == DialogResult.OK)
{
System.IO.File.WriteAllText(fo.FileName, _workingPO.GetXML(), Encoding.UTF8);
}
}

... you just get the last part of the extended extension. So you don't get to type in "testOne" and save a file named "testOne.po.xml". Darn.

The trick is easy enough though... just add in the DefaultExtension property...

using (SaveFileDialog fo = new SaveFileDialog())
{
fo.Filter = "PurchaseOrder xml (*.po.xml)|*.po.xml|xml|*.xml";
fo.FilterIndex = 0;
  fo.DefaultExt = ".po.xml";
fo.OverwritePrompt = true;
if (fo.ShowDialog() == DialogResult.OK)
{
System.IO.File.WriteAllText(fo.FileName, _workingPO.GetXML(), Encoding.UTF8);
}
}

... if the dialog is set to filter index 0 you get your correct "testOne.po.xml" file name. And, luckily, if the user picks filter index 1 the standard "testOne.xml" is created.

 
Hope that helps.

Robert Smith
Kirkland, WA



home     who is smith    contact smith     rss feed π
Since 1997 a place for my stuff, and it if helps you too then all the better smithvoice.com