How does WPF OpenFileDialog track directory of last opened file?

浪子不回头ぞ 提交于 2019-12-02 01:17:22
nick_w

On Windows 7 the recent file information is stored in the registry at this key:

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Comdlg32\OpenSaveMRU

Beneath this key are subkeys for the various file extensions (e.g., exe, docx, py, etc).

Now, if you want to read these values, this will get a list of all paths stored beneath the subkeys (adapted from here):

String mru = @"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU";
RegistryKey rk = Registry.CurrentUser.OpenSubKey(mru);
List<string> filePaths = new List<string>();

foreach (string skName in rk.GetSubKeyNames())
{
    RegistryKey sk = rk.OpenSubKey(skName);
    object value = sk.GetValue("0");
    if (value == null)
        throw new NullReferenceException();

    byte[] data = (byte[])(value);

    IntPtr p = Marshal.AllocHGlobal(data.Length);
    Marshal.Copy(data, 0, p, data.Length);

    // get number of data;
    UInt32 cidl = (UInt32)Marshal.ReadInt16(p);

    // get parent folder
    UIntPtr parentpidl = (UIntPtr)((UInt32)p);

    StringBuilder path = new StringBuilder(256);

    SHGetPathFromIDListW(parentpidl, path);

    Marshal.Release(p);

    filePaths.Add(path.ToString());
}

References:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!