Question : Export or download file from embeded resource Silverlight

Hi,
   I am develop an offline application using silverlight 3.0, am now faces need to save some form from embedded resource to the client. The form is excel and ms word format. The error show in   FileStream fileStream = new FileStream("", FileMode.CreateNew); error message is Attempt to access the method failed: System.IO.FileStream..ctor(System.String, System.IO.FileMode), expert pls help

private void btnDownload_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Filter = "Word Files | *.doc";
            saveDlg.DefaultExt = ".doc";

            if ((bool)saveDlg.ShowDialog())
            {
                using (Stream fs = saveDlg.OpenFile())
                {

                    Stream stream = this.GetType().Assembly.GetManifestResourceStream("application12." + txtDownloadFile.Text);
                    FileStream fileStream = new FileStream("application12." + txtDownloadFile.Text, FileMode.CreateNew);

                    const int size = 4096;
                    byte[] bytes = new byte[4096];
                    int numBytes;
                    while ((numBytes = stream.Read(bytes, 0, size)) > 0)
                    {
                        fileStream.Write(bytes, 0, numBytes);
                    }

                    stream.Close();
                    fileStream.Close();
                   
                 
                   
                    MessageBox.Show("File Saved");

                    popupDownload.IsOpen = false;
                }

            }
           
        }

Answer : Export or download file from embeded resource Silverlight

Consider this when using CreateNew flag:
"Specifies that the operating system should create a new file. This requires FileIOPermissionAccess..::.Write. If the file already exists, an IOException is thrown."

so u better use OpenOrCreate, or if u wish to always create new file, delete it first then use CreateNew flag.
another thing is write permissions, make sure u have write permissions and the file is not read only.
Random Solutions  
 
programming4us programming4us