简体   繁体   中英

how to set OpenSavePidMRUl default value using C#?

I have an application and I don't have access to source code. In the application, I open a OpenFileDialog to select an xml file. So, for an automation script, I want to change default OpenFileDialog location.

So that purpose I figured out windows restore last OpenFileDialog location in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidMRU\xml at register. So, in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidMRU\xml location there is a Default file when I changed the default file OpenFileDialog opening the address what I type in the Default file.

So, my question is how can I modify default file in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidMRU\xml using C#?

Regards

You can use this to edit your value in the register, putting the path you want to use:

RegistryKey key = 
  Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU\xml", true);

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

key.Close();

In order to set OpenSavePidMRUl default value, all you need to do is call method SetLastOpenSaveFile(...) from the code below:

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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM