0

Eventhough i specify a different location the file gets saved in mydocuments. How to resolve this issue. Pls share your ideas if any.Here is the code.

          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;
user1665707
  • 615
  • 3
  • 11
  • 24
  • Am using a different thread as i am getting "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made” error – user1665707 Feb 13 '13 at 10:25

2 Answers2

3

You are showing dialog assynchronously on new thread and code after starting the thread executes before dialog is shown (most of the time).

Either wait for thread completion or move saving to that thread after dialog is closed.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

Why You Are Showing saveFileDialog in different thread? if you show save dialog in diffrent thread fname = saveFileDialog.FileName; is always return null.dont use separate thread.or Call this event after thread start

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

Edited

Example

 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;
        }
    }
KF2
  • 9,887
  • 8
  • 44
  • 77