C# RegOpenKeyEx返回错误161
我很难重命名我的注册表项。我不知道,但一直在更改名称,所以我试图使用一个程序来自动执行。代码如下:C# RegOpenKeyEx返回错误161,c#,C#,我很难重命名我的注册表项。我不知道,但一直在更改名称,所以我试图使用一个程序来自动执行。代码如下: 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.Threa
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规则未重命名:
我引用的文章:
我希望有人能帮助。。。它仅适用于我的计算机。错误87是
错误\u错误\u参数
,表示传递给函数的一个或多个参数不正确
错误161是Error\u BAD\u PATHNAME
,表示您向函数传递了不正确的路径
我认为这两个错误都是因为在路径中使用了前导斜杠或尾随斜杠。因此,您将使用SOFTWARE\\Company
而不是例如\\SOFTWARE\\Company
。尝试以下更改,看看是否有效
删除了前导斜杠:
int resul = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Company", 0,0,out result);
int rosul = RegRenameKey(hKey, "SOFTWARE\\Company", "SOFTWARE\\Editado");
删除了尾部斜杠:
int resul = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Company", 0,0,out result);
int rosul = RegRenameKey(hKey, "SOFTWARE\\Company", "SOFTWARE\\Editado");
参考资料
KEY\u WOW64\u 32KEY
或KEY\u WOW64\u 64KEY
标志。