10

I thought using application settings would do the trick but I'm not getting it to work. This is what I have:

private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (fbFolderBrowser.ShowDialog() == DialogResult.OK)
            {
                // I want to open the last folder selected by the user here.
            }

When the user clicks on this button, I want to open the browse window to the last folder he accessed and save it. Next time he clicks on the button, it'll automatically select that folder.

I was thinking maybe I could use user variables where I can change at run-time but I'm not getting it to work. Can anyone give me a hand?

4

5 に答える 5

18

作成したプロジェクトのプロジェクト デザイナーの設定ページに移動し、アプリケーション内にフォルダー パス変数を追加します。以下のコードを追加して、最後に選択したフォルダー パスを復元します。

FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
folderBrowser.Description = "Select a folder to extract to:";
folderBrowser.ShowNewFolderButton = true;
folderBrowser.SelectedPath = Properties.Settings.Default.Folder_Path;
//folderBrowser.SelectedPath = project_name.Properties.Settings.Default.Folder_Path;

if (folderBrowser.ShowDialog() == DialogResult.OK)
{

    if (!String.IsNullOrEmpty(Properties.Settings.Default.Folder_Path))
        Properties.Settings.Default.Folder_Path = folderBrowser.SelectedPath;

    Properties.Settings.Default.Folder_Path = folderBrowser.SelectedPath;
    Properties.Settings.Default.Save();
}
于 2012-10-28T13:56:55.120 に答える
4

ユーザーが最後にアクセスしたフォルダーを見つけることができる場所は 2 つあります。

  1. Recent Files and Folders:ここで見つけることができます:C:\Documents and Settings\USER\Recent
  2. Registry: レジストリでここを見てください:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU

このスニペットを使用して見つけることができます。

public static string GetLastOpenSaveFile(string extention)
{
    RegistryKey regKey = Registry.CurrentUser;
    string lastUsedFolder = string.Empty;
    regKey = regKey.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU");

    if (string.IsNullOrEmpty(extention))
        extention = "html";

    RegistryKey myKey = regKey.OpenSubKey(extention);

    if (myKey == null && regKey.GetSubKeyNames().Length > 0)
        myKey = regKey.OpenSubKey(regKey.GetSubKeyNames()[regKey.GetSubKeyNames().Length - 2]);

    if (myKey != null)
    {
        string[] names = myKey.GetValueNames();
        if (names != null && names.Length > 0)
        {
            lastUsedFolder = (string)myKey.GetValue(names[names.Length - 2]);
        }
    }

    return lastUsedFolder;
}

また

Windows XP では、ファイルが保存されているディレクトリで [保存] を押すSaveFileDialogと、新しい現在の作業ディレクトリ ( のディレクトリ) として設定されEnvironment.CurrentDirectoryます。

このように、 を再度開くとFileDialog、以前と同じディレクトリで開かれます。

を設定することで、元の作業ディレクトリFileDialog.RestoreDirectory = trueを閉じたときにFileDialog復元されます。

Windows Vista/Seven では、動作は常にFileDialog.RestoreDirectory = true.

于 2012-08-20T14:25:13.570 に答える
2

アプリケーションの設定でうまくいきます。
より精巧なバージョンはこちら

文字列型の設定を使用する

各ボタンの設定を作成し、そこにパスを保存します。次に、設定を ofd.InitialPath として使用します

上記のコード例を使用して、これを試してください:

ソリューション エクスプローラーでアプリ名を右クリックし、[設定] タブをクリックします。 名前 = Button1Path タイプ = 文字列 スコープ = ユーザー

次にこれを使用します:

private void btnBrowse_Click(object sender, EventArgs e)
{
    fbFolderBrowser.InitialDirectory=this.Settings.Button1Path;
    if (fbFolderBrowser.ShowDialog() == DialogResult.OK)
    {
        // I want to open the last folder selected by the user here.
        this.Settings.Button1Path=fbFolderBrowser.SelectedPath
    }
}
于 2012-08-20T14:32:01.713 に答える
0

次のように、最後に選択したフォルダーを簡単に追跡できます。

public String LastSelectedFolder;

private void btnBrowse_Click(object sender, EventArgs e)
{
    fbFolderBrowser.InitialDirectory=this.Settings.Button1Path;
    if (fbFolderBrowser.ShowDialog() == DialogResult.OK)
    {
        // Save Last selected folder.
        LastSelectedFolder = fbFolderBrowser.SelectedPath;
    }
}
于 2012-10-11T14:39:27.433 に答える