Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

c# - SaveAs Dialog not saving file from network location

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:storeemail1.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 \serverstoreemail1.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?

question from:https://stackoverflow.com/questions/65926042/saveas-dialog-not-saving-file-from-network-location

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...