2013-02-13 83 views
0

雖然我指定了不同的位置,但文件被保存在mydocuments中。如何解決這個問題。請分享你的想法,如果有的話。這裏是代碼。文件沒有保存在winforms中的指定位置c#

  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; 
+3

爲什麼線? – BlueChippy 2013-02-13 06:39:40

+0

正在使用不同的線程,因爲我得到「當前線程必須設置爲單線程單元(STA)模式之前可以進行OLE調用」錯誤 – user1665707 2013-02-13 10:25:48

回答

3

你是顯示啓動線程後新的線程和代碼對話框assynchronously執行顯示之前對話框(大部分時間)。

要麼等待線程完成或關閉對話框後移至保存到該線程。

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; 
     } 
    } 
+0

正在使用不同的線程,因爲我越來越「當前線程必須設置爲單之前OLE調用線程單元(STA)模式可取得」 錯誤 – user1665707 2013-02-13 10:15:08

+0

化妝保存在UI線程顯示對話框中,它與'thread.SetApartmentState(ApartmentState.STA)你的線程;' – KF2 2013-02-13 10:49:59

+0

我憑着瞭解如何保存對話框中的UI線程..我該怎麼做? – user1665707 2013-02-13 12:11:28