文件未保存在winforms中的指定位置c#

时间:2013-02-13 06:38:12

标签: c# winforms savefiledialog

尽管我指定了一个不同的位置,文件将保存在mydocuments中。如何解决此问题。如果有的话,请分享您的想法。这是代码。

          if (externalButton.Checked == true)
            {
               // int i = 1;
                saveFileDialog.Title = "Save the Proofer Report";
                saveFileDialog.Filter = "Document Files (*.doc)|*.doc|Document Files (*.docx)|*.docx";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.InitialDirectory = "MyDocuments";
                saveFileDialog.FileName = "Proofer Report -- " +  Path.GetFileName((string)fileName) + ".doc";
                //i.tostring()
                saveFileDialog.DefaultExt = ".doc"; 


                saveFileDialog.ShowHelp = true;
                // saveFileDialog.ShowDialog();
                var thread = new Thread(new ParameterizedThreadStart(param => { saveFileDialog.ShowDialog(); }));
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                fname = saveFileDialog.FileName;

2 个答案:

答案 0 :(得分:3)

在对话框显示之前(大部分时间)开始执行之后,您将在新线程和代码上异步显示对话框。

在对话框关闭后等待线程完成或将保存移动到该线程。

答案 1 :(得分:0)

为什么您在不同的帖子中显示saveFileDialog? 如果你在不同的线程中显示保存对话框fname = saveFileDialog.FileName;总是返回null.dont使用单独的thread.or在线程启动后调用此事件

saveFileDialog1.FileOk += new CancelEventHandler(saveFileDialog1_FileOk);
 void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            string fname = null;
            fname = saveFileDialog1.FileName;
        }

<强>被修改

实施例

 public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            _SaveFileDialog.FileOk += new CancelEventHandler(_SaveFileDialog_FileOk);
        }
        string filename = null;
        SaveFileDialog _SaveFileDialog = new SaveFileDialog();
        private void savebtn_Click(object sender, EventArgs e)
        {
            _SaveFileDialog.Title = "Save the Proofer Report";
            _SaveFileDialog.Filter = "Document Files (*.doc)|*.doc|Document Files (*.docx)|*.docx";
            _SaveFileDialog.FilterIndex = 0;
            _SaveFileDialog.InitialDirectory = "MyDocuments";
            _SaveFileDialog.FileName = "Proofer Report -- .doc";
            _SaveFileDialog.DefaultExt = ".doc";
            _SaveFileDialog.ShowHelp = true;
            var thread = new Thread(new ParameterizedThreadStart(param => { _SaveFileDialog.ShowDialog(); }));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
        void _SaveFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            filename = _SaveFileDialog.FileName;
        }
    }