gpt4 book ai didi

openfiledialog - WPF OpenFileDialog 如何跟踪上次打开文件的目录?

转载 作者:行者123 更新时间:2023-12-02 22:38:06 27 4
gpt4 key购买 nike

据我们所知,WPF OpenFileDialog 不再更改应用程序的工作目录,并且 RestoreDirectory 属性“未实现”。但是,在后续打开时,其初始目录默认为上次打开的文件而不是原始工作目录,因此必须将此信息存储在某个地方。我想知道是否可以从用户代码中获取/设置它?

最佳答案

在 Windows 7 上,最近的文件信息存储在注册表的这个键中:

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

此键下是各种文件扩展名(例如,exedocxpy 等)的子键。

现在,如果您想读取这些值,这将获得存储在子键下的所有路径的列表(改编自 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());
}

引用资料:

关于openfiledialog - WPF OpenFileDialog 如何跟踪上次打开文件的目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11144770/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com