首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用OpenSavePidMRUl设置C#默认值?

如何使用OpenSavePidMRUl设置C#默认值?
EN

Stack Overflow用户
提问于 2022-03-10 13:12:31
回答 2查看 80关注 0票数 0

我有一个应用程序,我无法访问源代码。在应用程序中,我打开一个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#修改默认文件?

问候

EN

回答 2

Stack Overflow用户

发布于 2022-03-10 13:20:13

您可以使用它在寄存器中编辑您的值,并放置您想要使用的路径:

代码语言:javascript
复制
RegistryKey key = 
  Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU\xml", true);

key.SetValue("someValue", "someData"); //sets 'someData' in 'someValue' 

key.Close();
票数 2
EN

Stack Overflow用户

发布于 2022-05-05 10:36:09

为了设置OpenSavePidMRUl默认值,只需调用方法SetLastOpenSaveFile(.)来自以下代码:

代码语言:javascript
复制
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;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71424719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档