PRB: Unable to Use a "\" in the Key Name with the RegWrite Method of Windows Script Host (281309)
The information in this article applies to:
- Microsoft Windows Script Host 1.0
- Microsoft Windows Script Host 2.0
This article was previously published under Q281309 IMPORTANT: This article contains information about modifying the registry. Before you
modify the registry, make sure to back it up and make sure that you understand how to restore
the registry if a problem occurs. For information about how to back up, restore, and edit the
registry, click the following article number to view the article in the Microsoft Knowledge Base:
256986 Description of the Microsoft Windows Registry
SYMPTOMS
Due to the limitations of the RegWrite method of Windows Script Host (WSH) it is not possible to write a "\" (backslash) in a key name or value name.
CAUSE
The registry methods in Windows Script Host treat the backslash (\) as part of the key or value path instead of the name and will actually ignore extra backslashes such as in a UNC path (\\servername\sharename).
RESOLUTIONWARNING: If you use Registry Editor incorrectly, you may cause serious problems that may
require you to reinstall your operating system. Microsoft cannot guarantee that you can solve
problems that result from using Registry Editor incorrectly. Use Registry Editor at your own
risk.
Here are two possible ways to work around this problem:
- Using Windows Management Instrumentation (WMI):
Const HKEY_LOCAL_MACHINE As Long = &H80000002
Dim lRC
Dim sPath
Dim objRegistry
Set objRegistry = GetObject("winmgmts://./root/default:StdRegProv")
sPath = "SOFTWARE\MyKey"
lRC = objRegistry.SetStringValue(HKEY_LOCAL_MACHINE, sPath, _
"\\Server\Share", "Username")
If (lRC = 0) And (Err.Number = 0) Then
WScript.Echo "Write successful!"
Else
WScript.Echo "An error occurred. The return code is " & lRC & "."
End If
- Using RegObj.dll:
Dim RegObj, RegKey, RegValue
Dim key
Set RegObj = CreateObject("RegObj.Registry")
key = "\HKEY_LOCAL_MACHINE\SOFTWARE\MyKey"
val = "\\ServerName\Share"
data = "Username"
Reg_WriteValue = False
on error resume next
Set RegKey = RegObj.RegKeyFromString(key)
RegKey.Values.Add val, data, 1
if err.number <> 0 then
WScript.Echo "Error: " & err.number & " " & err.description
End If
set RegKey = nothing
set RegObj = nothing
STATUS
This behavior is by design.
REFERENCES
To download the latest WMI version, go to the following Microsoft Web site:
For more information about WMI, see the following MSDN Web site:
You can download RegObj.dll from the following Microsoft Web site:
Modification Type: | Major | Last Reviewed: | 5/2/2006 |
---|
Keywords: | kbDSWManage2003Swept kbprb KB281309 |
---|
|