windows 如何从命令行向注册表添加多行 REG_SZ 字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/153879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:21:06  来源:igfitidea点击:

How do I add a multiline REG_SZ string to the registry from the command line?

windowsregistrybatch-file

提问by morechilli

As part of a build setup on a windows machine I need to add a registry entry and I'd like to do it from a simple batch file.

作为 Windows 机器上构建设置的一部分,我需要添加一个注册表项,我想从一个简单的批处理文件中完成。

The entry is for a third party app so the format is fixed.

该条目用于第三方应用程序,因此格式是固定的。

The entry takes the form of a REG_SZ string but needs to contain newlines ie. 0xOA characters as separators.

该条目采用 REG_SZ 字符串的形式,但需要包含换行符,即。0xOA 字符作为分隔符。

I've hit a few problems.

我遇到了一些问题。

First attempt used regedit to load a generated .reg file. This failed as it did not seem to like either either long strings or strings with newlines. I discovered that export works fine import fails. I was able to test export as the third party app adds similar entries directly through the win32 api.

第一次尝试使用 regedit 加载生成的 .reg 文件。这失败了,因为它似乎不喜欢长字符串或带换行符的字符串。我发现导出工作正常导入失败。我能够测试导出,因为第三方应用程序直接通过 win32 api 添加了类似的条目。

Second attempt used the command REG ADD but I can't find anyway to add the newline characters everything I try just ends up with a literal string being added.

第二次尝试使用命令 REG ADD 但我无论如何都找不到添加换行符,我尝试的所有内容都以添加文字字符串结束。

采纳答案by tloach

You could create a VBScript(.vbs) file and just call it from a batch file, assuming you're doing other things in the batch other than this registry change. In vbscript you would be looking at something like:

您可以创建一个 VBScript(.vbs) 文件,然后从批处理文件中调用它,假设您正在批处理中执行除此注册表更改之外的其他操作。在 vbscript 中,你会看到类似的东西:

set WSHShell = CreateObject("WScript.Shell")  
WSHShell.RegWrite "HKEY_LOCAL_MACHINE\SOMEKEY", "value", "type"

You should be able to find the possible type values using Google.

您应该能够使用 Google 找到可能的类型值。

回答by Factor Mystic

If you're not constrained to a scripting language, you can do it in C# with

如果您不受脚本语言的限制,则可以在 C# 中使用

Registry.CurrentUser.OpenSubKey(@"software\classes\something", true).SetValue("some key", "sometext\nothertext", RegistryValueKind.String);

回答by kbulgrien

You can import multiline REG_SZ strings containing carriage return (CR) and linefeed (LF) end-of-line (EOL) breaks into the registry using .reg files as long as you do not mind translating the text as UTF-16LE hexadecimal encoded data. To import a REG_SZ with this text:

您可以使用 .reg 文件将包含回车 (CR) 和换行 (LF) 换行符 (EOL) 的多行 REG_SZ 字符串导入注册表,只要您不介意将文本翻译为 UTF-16LE 十六进制编码数据. 要使用此文本导入 REG_SZ:

1st Line
2nd Line

You might create a file called MULTILINETEXT.REG that contains this:

您可以创建一个名为 MULTILINETEXT.REG 的文件,其中包含以下内容:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Environment]
"MULTILINETEXT"=hex(1):31,00,73,00,74,00,20,00,4c,00,69,00,6e,00,65,00,0d,00,0a,00,\
32,00,6e,00,64,00,20,00,4c,00,69,00,6e,00,65,00,0d,00,0a,00,\
00,00

To encode ASCII into UTF-16LE, simply add a null byte following each ASCII code value. REG_SZ values must terminate with a null character (,00,00) in UTF-16LE notation.

要将 ASCII 编码为 UTF-16LE,只需在每个 ASCII 代码值后面添加一个空字节。REG_SZ 值必须,00,00以 UTF-16LE 表示法中的空字符 ( )结尾。

Import the registry change in the batch file REG.EXE IMPORT MULTILINETEXT.REG.

导入批处理文件中的注册表更改REG.EXE IMPORT MULTILINETEXT.REG

The example uses the Environment key because it is convenient, not because it is particularly useful to add such data to environment variables. One may use RegEdit to verify that the imported REG_SZ data contains the CRLF characters.

该示例使用 Environment 键是因为它很方便,而不是因为将此类数据添加到环境变量中特别有用。可以使用 RegEdit 来验证导入的 REG_SZ 数据是否包含 CRLF 字符。