【发布时间】:2017-03-26 00:06:45
【问题描述】:
我很难重命名我的注册表的一个键。我不知道,但一直在更改名称,所以我试图使用一个程序来自动完成。代码如下:
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
private static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(-2147483646);
[DllImport("advapi32")]
public static extern int RegRenameKey(SafeRegistryHandle hKey, [MarshalAs(UnmanagedType.LPWStr)] string oldname,
[MarshalAs(UnmanagedType.LPWStr)] string newname);
[DllImport("Advapi32.dll", EntryPoint = "RegOpenKeyExW", CharSet = CharSet.Unicode)]
public static extern int RegOpenKeyEx(IntPtr hKey, [In] string lpSubKey, int ulOptions, int samDesired, out IntPtr phkResult);
static void Main(string[] args)
{ //mhmmm si, todo esta bien , bueno ire a ver lo que iba oka ver
///Estas intentando renombrar una clave de registro una la crpeta que la contiene...
IntPtr result;
SafeRegistryHandle hKey = null;//no es necesario, esta funcuonando, si no, no me hubiere retorando un int
hKey = new SafeRegistryHandle(HKEY_LOCAL_MACHINE,true);
int resul = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "\\SOFTWARE\\Company", 0,0,out result);
Console.WriteLine(resul);
int rosul = RegRenameKey(hKey, "SOFTWARE\\Company\\", "SOFTWARE\\Editado\\");
Console.WriteLine(rosul);
Console.ReadLine(); //Ok a ver dejamever unos ejemplos de advapi, los tienes ahi? mierdaaa no tiees ideas de los peos que se acaba de tirar mi perro
}
}
}
问题是当我打开钥匙时:
int resul = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "\\SOFTWARE\\Company", 0,0,out result);
由于某种原因,我没有很好地打开钥匙。错误返回我:
我的 regedit 规则没有重命名:
我参考的文章:
http://blogs.microsoft.co.il/pavely/2015/09/29/regrenamekey-hidden-registry-api/
我希望有人能帮上忙……它只适用于我的电脑。
【问题讨论】:
-
请注意,如果您使用的是 64 位操作系统,您的屏幕截图将显示 64 位注册表配置单元,而不是 32 位。 .NET 通常会根据您的应用程序位数重定向您的访问,但在这种情况下,由于您正在访问 Win32 API,因此您可能需要传递
KEY_WOW64_32KEY或KEY_WOW64_64KEY标志。
标签: c#