我有一个应用程序,我无法访问源代码。在应用程序中,我打开一个OpenFileDialog来选择一个xml文件。因此,对于自动化脚本,我希望更改默认的OpenFileDialog位置。
因此,我想出了windows还原HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidMRU\xml中最后一个OpenFileDialog位置的方法。因此,在HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidMRU\xml位置,当我更改默认文件OpenFileDialog时,会出现一个默认文件,打开我在默认文件中键入的地址。
因此,我的问题是如何使用HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidMRU\xml使用C#修改默认文件?
问候
发布于 2022-03-10 13:20:13
您可以使用它在寄存器中编辑您的值,并放置您想要使用的路径:
RegistryKey key =
Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU\xml", true);
key.SetValue("someValue", "someData"); //sets 'someData' in 'someValue'
key.Close();发布于 2022-05-05 10:36:09
为了设置OpenSavePidMRUl默认值,只需调用方法SetLastOpenSaveFile(.)来自以下代码:
public static bool SetLastOpenSaveFile(String csExtension, String csLastUsedFolder)
{
String csKey = String.Format(@"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU\{0}", csExtension);
RegistryKey extMRUKey;
RegistryDisposition? disposition;
if (!RegistryFunctions.RegOpenKeyEx(Registry.CurrentUser, csKey, RegistryOptions.None, RegistryKeyPermissionCheck.ReadWriteSubTree, out extMRUKey, out disposition))
return false;
// Get index from MRUListEx
String mruListValue = "MRUListEx";
byte[] lpData;
try
{
lpData = extMRUKey.GetValue(mruListValue) as byte[];
}
catch (Exception)
{
return false;
}
if (lpData == null)
return false;
if (lpData.Length < 4)
return false;
Int32 dwLastIndex = BitConverter.ToInt32(lpData, 0);
String csValueName = String.Format("{0}", dwLastIndex);
IntPtr pidl = IntPtr.Zero;
try
{
pidl = Pinvoke.ILCreateFromPathW(csLastUsedFolder);
int size = Pinvoke.ILGetSize(pidl);
var bytes = new byte[size];
Marshal.Copy(pidl, bytes, 0, size);
extMRUKey.SetValue(csValueName, bytes);
return true;
}
catch
{
return false;
}
finally
{
if (pidl != IntPtr.Zero)
Pinvoke.ILFree(pidl);
}
}
public static void GetLastOpenSaveFile(String csExtension, out String csLastUsedFolder)
{
csLastUsedFolder = String.Empty;
String csKey = String.Format(@"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU\{0}", csExtension);
RegistryKey extMRUKey;
RegistryDisposition? disposition;
if (!RegistryFunctions.RegOpenKeyEx(Registry.CurrentUser, csKey, RegistryOptions.None, RegistryKeyPermissionCheck.ReadSubTree, out extMRUKey, out disposition))
return;
// Get index from MRUListEx
String mruListValue = "MRUListEx";
byte[] lpData;
try
{
lpData = extMRUKey.GetValue(mruListValue) as byte[];
}
catch (Exception)
{
return;
}
if (lpData == null)
return;
if (lpData.Length < 4)
return;
Int32 dwLastIndex = BitConverter.ToInt32(lpData, 0);
String csValueName = String.Format("{0}", dwLastIndex);
lpData = extMRUKey.GetValue(csValueName) as byte[];
if (lpData != null && lpData.Length > 0)
{
System.Text.StringBuilder sbPath = new System.Text.StringBuilder(1024);
try
{
IntPtr ptrRet;
Pinvoke.SHGetMalloc(out ptrRet);
Object obj = Marshal.GetTypedObjectForIUnknown(ptrRet, typeof(IMalloc));
IMalloc pMalloc = (IMalloc)obj;
// Also allocate memory for the final null SHITEMID.
IntPtr hpidlNew = pMalloc.Alloc(sizeof(byte) + (uint)lpData.Length);
try
{
if (hpidlNew != IntPtr.Zero)
{
Marshal.Copy(lpData, 0, hpidlNew, lpData.Length);
if (Pinvoke.SHGetPathFromIDListW(hpidlNew, sbPath))
csLastUsedFolder = sbPath.ToString();
}
}
finally
{
if (hpidlNew != IntPtr.Zero)
pMalloc.Free(hpidlNew);
}
}
catch (Exception)
{ }
}
}
public enum RegistryDisposition
{
/// <summary>
/// The key did not exist and was created.
/// </summary>
REG_CREATED_NEW_KEY = 0x00000001,
/// <summary>
/// The key existed and was simply opened without being changed.
/// </summary>
REG_OPENED_EXISTING_KEY = 0x00000002
}
public static class RegistryFunctions
{
public static bool RegOpenKeyEx(RegistryKey parentKey, string subKey, RegistryOptions options, RegistryKeyPermissionCheck permissionCheck, out RegistryKey result, out RegistryDisposition? disposition)
{
result = null;
disposition = null;
try
{
result = parentKey.OpenSubKey(subKey, permissionCheck);
if (result != null)
{
disposition = RegistryDisposition.REG_OPENED_EXISTING_KEY;
return true;
}
}
catch (Exception)
{
}
return false;
}
public static bool RegCreateKeyEx(RegistryKey parentKey, string subKey, RegistryOptions options, RegistryKeyPermissionCheck permissionCheck, out RegistryKey result, out RegistryDisposition? disposition)
{
try
{
result = parentKey.OpenSubKey(subKey, permissionCheck);
if (result != null)
{
disposition = RegistryDisposition.REG_OPENED_EXISTING_KEY;
return true;
}
}
catch (Exception)
{
}
try
{
result = parentKey.CreateSubKey(subKey, permissionCheck, options);
if (result != null)
{
disposition = RegistryDisposition.REG_CREATED_NEW_KEY;
return true;
}
}
catch (Exception)
{
}
disposition = null;
result = null;
return false;
}
public static bool RegSetValueEx(RegistryKey key, string valueName, object value)
{
try
{
key.SetValue(valueName, value);
return true;
}
catch (Exception)
{
return false;
}
}
}https://stackoverflow.com/questions/71424719
复制相似问题