HowTo: Save a file from Silverlight using the SaveFileDialog

Saving a file from Silverlight using the SaveFileDialog, added in Silverlight 3, is easy. If you’re used to desktop development however, you might find yourself getting a SecurityException with the message ‘File operation not permitted. Access to path ‘xxx’ is denied.’. Here’s why:

In desktop development, you’re used to getting a filename from a SaveFileDialog. Next, you start doing whatever you need to be doing to the file, based on the filename. This would look something like this:

SaveFileDialog saveFileDialog = new SaveFileDialog();

if (saveFileDialog.ShowDialog() == true)
{
    StreamWriter streamWriter = new StreamWriter(saveFileDialog.SafeFileName);
    streamWriter.Write("Follow me on twitter: @rickvdbosch");
    streamWriter.Flush();
    streamWriter.Close();
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

This results in the SecurityException. First time I saw the property, I genuinely thought it said SaveFileName. But, as you can see, it says SafeFileName. It is named safe, because the returned file name has all file path information removed for security purposes.

In Silverlight, if you want the above functionality, it should look something like this:

SaveFileDialog saveFileDialog = new SaveFileDialog();

if (saveFileDialog.ShowDialog() == true)
{
    StreamWriter streamWriter = new StreamWriter(saveFileDialog.OpenFile());
    streamWriter.WriteLine("Follow me on twitter: @rickvdbosch");
    streamWriter.Flush();
    streamWriter.Close();
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }The OpenFile method opens the file specified by the SafeFileName returning a Stream, giving you the opportunity to save whatever you want to save over there.

Hope this helps.

On a side note: the ‘== true’ part in the if condition is needed in this case. Because the ShowDialog method returns a nullable boolean it is not enough to use saveFileDiaolog.ShowDialog() in the condition. Just so you know… :)

‘Cannot find entry to delete’ deleting files from a Zip with (SL) SharpZipLib

For a project I’ll probably dedicate a post on this blog to in the future, I needed a ZIP library for Silverlight. After asking around on twitter (@rickvdbosch) I was pointed towards the SharpZipLib port on Codeplex, found on http://slsharpziplib.codeplex.com.

Basically what I wanted to do was replace a file inside the ZIP file with my version of that file. The file was located in a specific folder inside the ZIP and could be found by either iterating through the ZipEntries in the ZIP, or by getting it based on its name. But deleting the file gave me a ZipException with the error message ‘Cannot find entry to delete’. When I added a file myself, it could be deleted just fine, but the ZIP just did not want the existing file to be removed.

After searching around on some forums, I found there was a new development version of SharpZipLib which might solve the problem. But that was the full version, not the Silverlight port. So I searched around some more and found something about subfolders.

As it turns out, and existing file can be deleted when it is located in the root of the ZIP. Apparently there’s some bug keeping you from deleting files in subfolders. Anyhow, moving the file I need updated to the root of the ZIP solved my problem for me (for now), so I can continue my project. If and when slsharpziplib is updated in the future, I’ll have a look if they solved this in the port too…

Hope this helps.