1

I have a Save button that makes a query to a server which returns a filepath for an email on a shared drive, like "F:\store\email1.eml"

private void SaveAsBtn_Click(object sender, RoutedEventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                connection.Open();
                using (var cmd = new SqlCommand("spGetDoc", connection) { CommandType = System.Data.CommandType.StoredProcedure })
                {
                    SqlParameter param = new SqlParameter("@GUID", ConfirmedGuidBox.Text);
                    cmd.Parameters.Add(param);

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            string fileSource = rdr.GetString(1);

                            Stream myStream;
                            SaveFileDialog saveFileDialog = new SaveFileDialog();
                            saveFileDialog.Filter = "Emails|*.eml";
                            saveFileDialog.FileName = fileSource;
                            saveFileDialog.ShowDialog();
                        }
                    }
                }
            }
        }

This opens a SaveAs dialog as expected, but I can't get it to load the file correctly. The preset filename evaluates to \\server\store\email1.emlc#, which if you click Save on will attempt to save the file in the same shared location, rather than the location navigated to inside the Dialog. Shortening the full path to just email1.eml means nothing gets saved.

It appears saveFileDialog.FileName doesn't actually open the file in fileSource, just sets the default name. How can I get this to work, so that I'm able to save a copy of the file specified in the database query?

ancsjs
  • 37
  • 6
  • 2
    The dialog does not open the file, it just lets the user select a file name. You have to open the file `saveFileDialog.FileName` yourself after ShowDialog returns true. – Klaus Gütter Jan 27 '21 at 19:55
  • @KlausGütter The mechanic I'm trying to imitate is similar to how you're able to save a copy of the html file of a webpage in Firefox by rightclicking "save as", with the file autopopulated. The point of the saving function in my program becomes a bit obsolete if the user has to go find the file anyway. So this isn't possible in WPF? – ancsjs Jan 27 '21 at 20:00
  • **Don't do complex or blocking actions inside the reader loop** Get the data back into the client app, and do anything you need after – Charlieface Jan 27 '21 at 22:03

1 Answers1

0

Sounds like you are trying to set the initial directory the filesave dialogs opens to. See Setting the initial directory of an SaveFileDialog?

Rob
  • 628
  • 5
  • 9
  • I'm not so sure. Setting the filename and initial directory still doesn't seem to load the actual file itself into the dialog. – ancsjs Jan 27 '21 at 21:22
  • Perhaps you can explain better what you are trying to do then, here is a simple test in powershell you can perform `Add-Type -AssemblyName PresentationFramework` `$sfd = New-Object 'Microsoft.Win32.SaveFileDialog'` `#this will show no file in the filename of the dialog` `$sfd.ShowDialog()` `$sfd.InitialDirectory = "C:\Temp"` `$sfd.FileName = "testFile.test"` `#now it will default to the C:\Temp directory and show testFile.test as the filename` `$sfd.ShowDialog()` – Rob Jan 28 '21 at 01:29
  • A question of interest @ancsjs: if you already have the full path to the file, why are you prompting the user where to save to? – Rob Jan 28 '21 at 01:35