2011-04-21 58 views
2

我有一個按鈕,一旦點擊使用stremReader讀取文本文件,並保存文件的文件夾瀏覽器對話框。一旦我保存了文件並再次單擊該按鈕,我收到一條錯誤消息,說它找不到該文本文件,並嘗試從保存上一個文檔的路徑中讀取該文本文件。C#folderbrowser對話框問題

有什麼辦法可以解決這個問題嗎?

下面是代碼的一部分:

所有你可能想發佈一些代碼的
private void Invoice_Load(object sender, EventArgs e) 
{ 
    try 
    { 
     StreamReader sr = new StreamReader(@"../../DatabasePath"); 
     dataBase = sr.ReadLine(); 

     if (dataBase == null) 
     { 
      MessageBox.Show("Please use this to choose the location of the database."); 
      Process.Start(@"..\..\DatabaseChooser.exe"); 
      ready = false; 
     } 

     if (!ready) 
     { 
      while (IsProcessOpen("DatabaseChooser")) 
      { 
       ready = false; 
      } 
      ready = true; 

      if (ready) 
      { 
       doIfReady(); 
      } 
     } 

     else if (ready) 
     { 
      doIfReady(); 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

private void btnCreateInvoice_Click(object sender, EventArgs e) 
{ 
    int SelectColumnIndex = 5; 

    foreach (DataGridViewRow row in dataGridViewInvoice.Rows) 
    { 
     if (row.Cells[SelectColumnIndex].Value != null && 
       Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true) 
     { 
      foreach (DataGridViewCell cell in row.Cells) 
      { 
       if (cell.OwningColumn.Index != SelectColumnIndex) 
       { 
        data += (cell.Value + "   "); // do some thing        
       } 
      } 
      data += System.Environment.NewLine; 
      total += (int)row.Cells["TotPrice"].Value; 
     } 
    } 

    MessageBox.Show("Please choose your invoice template", "Template"); 
    OpenFileDialog op = new OpenFileDialog(); 
    op.ShowHelp = true; 
    op.Filter = "Microsoft Word Documents 97-2003 (*.doc)|*.doc|Microsoft Word 2007 (*.docx)|*.docx"; 
    if (op.ShowDialog() == DialogResult.Cancel) 
    { 
     this.Hide(); 
    } 

    MessageBox.Show("Please choose where you want to save the invoice", "Save"); 
    FolderBrowserDialog fd = new FolderBrowserDialog(); 
    fd.Description = "Please choose"; 
    if (fd.ShowDialog() == DialogResult.Cancel) 
    { 
     this.Hide(); 
    } 

    string path = fd.SelectedPath + "\\" + txtFileName.Text + ".doc"; 

    CreateWordDoc(op.FileName, path); 
} 
+6

發表您寫下的代碼.. – Marco 2011-04-21 13:24:27

+0

您正在尋找哪個文本文件,原始文件還是您認爲保存的文件?所有這些操作都發生在您點擊兩次的按鈕上? – 2011-04-21 13:27:29

回答

0

第一。其次,您應該使用SaveFileDialog來保存文件而不是FolderBrowser。

+0

我剛剛發佈的代碼 – Jason 2011-04-21 13:32:03

0

我想你應該使用絕對路徑改變這部分

StreamReader sr = new StreamReader(@"../../DatabasePath"); 


例如:

string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 

這裏dir是主要的exe DIR。
當你使用OpenDialog當前路徑可以改變,所以你不會再找到你的路徑../../

只是另一回事:當您使用Process.Start(@"..\..\DatabaseChooser.exe");您等待您的過程完成:我認爲如果您創建Process ps並使用ps.WaitForExit()可以做得更好。

+0

好吧非常感謝所有的幫助!非常感謝! – Jason 2011-04-21 13:40:58

+0

@Jason:我用一個例子編輯我的代碼 – Marco 2011-04-21 13:48:44

+0

嗨Marco,它仍然沒有工作。當我第一次點擊按鈕時,當再次點擊按鈕時,它會嘗試從我的桌面讀取文本文件,並將其設置爲使用絕對路徑。請helkp – Jason 2011-04-21 13:56:30

0

我假設你遇到的麻煩:

StreamReader sr = new StreamReader(@"../../DatabasePath"); 

將其更改爲:

File f = new File(@"../../DatabasePath"); 

然後做一個f.GetAbsolutePath找出它實際上是獲取文件。

一些更多的評論:

if (op.ShowDialog() == DialogResult.Cancel) 
{ 
    this.Hide();      
} 

如果用戶點擊取消代碼仍然會嘗試運行。並嘗試CreateWordDoc。