值写入到注册表用C# [英] Writing values to the registry with C#

查看:114
本文介绍了值写入到注册表用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建一个C#应用程序,它会监视注册表的更改,并将它们写回到用户登录注册表下一次的过程。



到目前为止,我已经得到了它的变化监测,报告他们,写蜂巢,key和value到一个文本文件中。我现在在哪里,我需要把它们值出来的文件,并把它们放回注册表中的位置。现在,我已经看了各种教程,但没有一个能回答这个问题/问题我有,所以比如我希望更改注册表项是:



HKEY_USERS \S-1-5-21-2055990625-1247778217-514451997-41655\Software\Microsoft\Windows NT\CurrentVersion\Windows消息Subsystem\Profiles\Outlook\0a0d020000000000c000000000000046的01020402和值载有



中的内容

我希望能够做的就是写回到那个并将值更改为合适。我现在有3串,一个含有关键的位置,一个值和最终是价值的内容,但我可以很容易地改变字符串操作来获得,并没有得到任何我需要或不需要。所以,如果有人能为我提供一种方式来写,这将不胜感激。



PS让你知道,那paticular值是我转换成字符串一个二进制值进行存储。如果您需要任何进一步的信息,请让我知道。



任何帮助,将不胜感激....谢谢



修改的代码目前我使用的:

 公共类章
{
公共无效写入(字符串键,字符串VALUENAME,字符串值)
{
字节[] =的byteValue System.Text.Encoding.UTF8.GetBytes(值);

Registry.SetValue(键,VALUENAME,价值RegistryValueKind.Binary);
}
}


解决方案

我M猜你只需要找到合适的类用来写入注册表。使用这个类使得它相对简单。 ?这是你正在寻找所有

 字符串键= @HKEY_CLASSES_ROOT\.sgdk2 
字符串VALUENAME =的String.Empty; //(默认)值
字符串值=sgdk2file;

Microsoft.Win32.Registry.SetValue(键,VALUENAME,价值
Microsoft.Win32.RegistryValueKind.String);

要一个十六进制字符串转换成二进制数据:

 字节的静态[] HexToBin(十六进制字符串)
{
VAR的结果=新的字节[hex.Length / 2];
表示(INT I = 0; I&下; hex.Length; I + = 2)
{
结果[I / 2] = byte.Parse(hex.Substring(1,2 ),System.Globalization.NumberStyles.HexNumber);
}
返回结果;
}

如果您需要再次看到这些字节为十六进制,你可以使用如下代码这样的:

 静态字符串BytesToHex(字节[]字节)
{
System.Text.StringBuilder SB =新的StringBuilder();
的for(int i = 0; I< bytes.Length;我++)
{
sb.Append(字节[I]的ToString(X2));
}
返回sb.ToString();
}



由于此代码演示,表示为十六进制0E,FF和10字节得到转换为二进制00001110,分别为11111111和00010000。

 静态无效的主要(字串[] args)
{
字节[]字节= HexToBin(0eff10);
Console.WriteLine(BytesToBinaryString(字节));
}

静态的byte [] HexToBin(十六进制字符串)
{
VAR的结果=新的字节[hex.Length / 2];
表示(INT I = 0; I&下; hex.Length; I + = 2)
{
结果[I / 2] = byte.Parse(hex.Substring(1,2 ),System.Globalization.NumberStyles.HexNumber);
}
返回结果;
}

静态字符串BytesToBinaryString(字节[]字节)
{
变种BA =新System.Collections.BitArray(字节);
System.Text.StringBuilder SB =新的StringBuilder();
的for(int i = 0; I< ba.Length;我++)
{
INT byteStart =(I / 8)* 8;
INT位= 7 - 我8%;
sb.Append(BA [byteStart +位]'1':'0');
如果(ⅰ%8 == 7)
sb.Append('');
}
返回sb.ToString();
}


I'm in the process of creating a C# application which will monitor changes made to the registry and write them back to the registry next time the user logs on.

So far I've got it monitoring changes, reporting them, writing the hive, key and value to a text file. I'm now at the point where I need to take them values out of the file and place them back into the registry. Now I've looked at various tutorials but none have been able to answer the question/problem I have, so for example the registry key I wish to change is:

HKEY_USERS\S-1-5-21-2055990625-1247778217-514451997-41655\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\0a0d020000000000c000000000000046 the value of 01020402 and the contents contained within that

What I want to be able to do is write back to that and change the value as appropriate. I currently have 3 strings, one contains the key location, one the value and the final is the contents of the value, although I could easily change the string manipulation to get and not get whatever I need or don't need. So if someone could provide me with a way to write to that it would be appreciated.

P.S so you know, that paticular value is a binary value that I converted to string for storage. If you require any further information please let me know.

Any help would be appreciated....thanks

EDIT Code I'm currently using:

public class reg
{
    public void write(string key, string valueName, string value)
    {
        Byte[] byteValue = System.Text.Encoding.UTF8.GetBytes(value);

        Registry.SetValue(key, valueName, value, RegistryValueKind.Binary);
    }
}

解决方案

I'm guessing you just need to find the right class to use to write to the registry. Using this class makes it relatively simple. Is this all you're looking for?

string key = @"HKEY_CLASSES_ROOT\.sgdk2";
string valueName = string.Empty; // "(Default)" value
string value = "sgdk2file";

Microsoft.Win32.Registry.SetValue(key,valueName, value,
   Microsoft.Win32.RegistryValueKind.String);

To convert a hex string into binary data:

static byte[] HexToBin(string hex)
{
   var result = new byte[hex.Length/2];
   for (int i = 0; i < hex.Length; i += 2)
   {
      result[i / 2] = byte.Parse(hex.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
   }
   return result;
}

If you need to see these bytes as hexadecimal again, you can use code like this:

static string BytesToHex(byte[] bytes)
{
   System.Text.StringBuilder sb = new StringBuilder();
   for (int i = 0; i < bytes.Length; i++)
   {
      sb.Append(bytes[i].ToString("x2"));
   }
   return sb.ToString();
}

As this code demonstrates, the bytes represented as hex 0e, ff and 10 get converted to binary 00001110, 11111111 and 00010000 respectively.

static void Main(string[] args)
{
   byte[] bytes = HexToBin("0eff10");
   Console.WriteLine(BytesToBinaryString(bytes));
}

static byte[] HexToBin(string hex)
{
   var result = new byte[hex.Length / 2];
   for (int i = 0; i < hex.Length; i += 2)
   {
      result[i / 2] = byte.Parse(hex.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
   }
   return result;
}

static string BytesToBinaryString(byte[] bytes)
{
   var ba = new System.Collections.BitArray(bytes);
   System.Text.StringBuilder sb = new StringBuilder();
   for (int i = 0; i < ba.Length; i++)
   {
      int byteStart = (i / 8) * 8;
      int bit = 7 - i % 8;
      sb.Append(ba[byteStart + bit] ? '1' : '0');
      if (i % 8 == 7)
         sb.Append(' ');
   }
   return sb.ToString();
}

这篇关于值写入到注册表用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆