2013-04-18 39 views
6

如何使我的應用程序商店在openFileDialog中打開的最後一條路徑以及新打開後恢復?如何保存openFileDialog中的最後一個文件夾?

OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    acc_path = openFileDialog1.FileName; 
    Settings.Default.acc_path = acc_path; 

    foreach (string s in File.ReadAllLines(openFileDialog1.FileName)) 
    { 
     accs.Enqueue(s); 
    } 
    label2.Text = accs.Count.ToString(); 
} 

回答

4

我認爲這將是足夠你使用SetCurrentDirectory到STE當前目錄的OS。所以在下一個對話框打開它會選擇該路徑。

或者乾脆將路徑保存到您的應用程序的某個變量中,然後使用
FileDialog.InitialDirectory屬性。

1

可以使用InitialDirectory屬性:http://msdn.microsoft.com/fr-fr/library/system.windows.forms.filedialog.initialdirectory.aspx

OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
openFileDialog1.InitialDirectory = previousPath; 
if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    previousPath = Path.GetDirectoryName(openFileDialog1.FileName); 
    acc_path = openFileDialog1.FileName; 
    Settings.Default.acc_path = acc_path; 

    foreach (string s in File.ReadAllLines(openFileDialog1.FileName)) 
    { 
     accs.Enqueue(s); 
    } 
    label2.Text = accs.Count.ToString(); 
} 
7

更改設置後,你必須調用

Settings.Default.Save(); 

,並打開打開文件對話框,然後設置

openFileDialog1.InitialDirectory = Settings.Default.acc_path; 
+1

我個人很喜歡這種方法,因爲我沒有在多個表單中重複使用同一個文件選擇器實例。 – Joel

1

以下是您需要確保OpenFileDialog將在用戶上次選擇的目錄中打開的時間,以及您的應用程序生命週期內的所有時間。

OpenFileDialog OpenFile = new OpenFileDialog(); 
OpenFile.RestoreDirectory = false; 
+1

也不要設置InitialDirectory屬性。 –

+0

...但我認爲你的意思是:OpenFile.RestoreDirectory = true; – Viking

0

我發現你所要做的不是設置初始目錄,而且對話框會記住你上次保存/打開的位置。這甚至在應用程序關閉並重新打開後也會記住。嘗試使用初始目錄註釋掉的代碼。上面的許多建議也會起作用,但如果您不想要其他功能,則只需執行此操作即可。

private void button1_Click(object sender, EventArgs e) 
    { 
     Stream myStream = null; 
     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 

     //openFileDialog1.InitialDirectory = "c:\\"; 
     openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
     openFileDialog1.FilterIndex = 2; 

     if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      try 
      { 
       if ((myStream = openFileDialog1.OpenFile()) != null) 
       { 
        using (myStream) 
        { 
         // Insert code to read the stream here. 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); 
      } 
     } 
    } 
0

如果您使用

Dim myFileDlog As New OpenFileDialog() 

,那麼你可以用它來恢復上次目錄

myFileDlog.RestoreDirectory = True 

這不是

myFileDlog.RestoreDirectory = False 
0

我知道這是一個一點舊線程,但我無法找到解決方案我喜歡這個相同的問題,所以我開發了我自己的。我在WPF中這樣做,但它應該在Winforms中幾乎相同。

本質上,我使用app.config文件來存儲我的程序的最後一個路徑。

當我的程序啓動時,我讀取配置文件並保存到全局變量。下面是我的程序啓動時調用的類和函數。

public static class Statics 
{ 
    public static string CurrentBrowsePath { get; set; } 

    public static void initialization() 
    { 
     ConfigurationManager.RefreshSection("appSettings"); 
     Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

     CurrentBrowsePath = ConfigurationManager.AppSettings["lastfolder"]; 
    } 
} 

接下來我有打開的文件瀏覽對話框,並設置InitialDirectory屬性什麼存儲在配置文件中的按鈕。希望這有助於任何人使用Google搜索。

private void browse_Click(object sender, RoutedEventArgs e) 
    { 
     OpenFileDialog open_files_dialog = new OpenFileDialog(); 
     open_files_dialog.Multiselect = true; 
     open_files_dialog.Filter = "Image files|*.jpg;*.jpeg;*.png"; 
     open_files_dialog.InitialDirectory = Statics.CurrentBrowsePath; 

     try 
     { 
      bool? dialog_result = open_files_dialog.ShowDialog(); 

      if (dialog_result.HasValue && dialog_result.Value) 
      { 
       string[] Selected_Files = open_files_dialog.FileNames; 

       if (Selected_Files.Length > 0) 
       { 
        ConfigWriter.Update("lastfolder", System.IO.Path.GetDirectoryName(Selected_Files[0])); 
       } 

       // Place code here to do what you want to do with the selected files. 
      } 
     } 
     catch (Exception Ex) 
     { 
      MessageBox.Show("File Browse Error: " + Environment.NewLine + Convert.ToString(Ex)); 
     } 
    } 
相關問題