Microsoft KB Archive/281309

= PRB: Unable to Use a &quot;\&quot; in the Key Name with the RegWrite Method of Windows Script Host =

Article ID: 281309

Article Last Modified on 12/28/2006

-

APPLIES TO


 * Microsoft Windows Scripting Host 2.5
 * Microsoft Windows Scripting Host 2.5

-



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 &quot;\&quot; (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).



RESOLUTION
WARNING: 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(&quot;winmgmts://./root/default:StdRegProv&quot;)

sPath = &quot;SOFTWARE\MyKey&quot; lRC = objRegistry.SetStringValue(HKEY_LOCAL_MACHINE, sPath, _                               &quot;\\Server\Share&quot;, &quot;Username&quot;) If (lRC = 0) And (Err.Number = 0) Then WScript.Echo &quot;Write successful!&quot; Else WScript.Echo &quot;An error occurred. The return code is &quot; & lRC & &quot;.&quot; End If

  Using RegObj.dll: Dim RegObj, RegKey, RegValue Dim key Set RegObj = CreateObject(&quot;RegObj.Registry&quot;)

key = &quot;\HKEY_LOCAL_MACHINE\SOFTWARE\MyKey&quot; val = &quot;\\ServerName\Share&quot; data = &quot;Username&quot; 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 &quot;Error: &quot; & err.number & &quot; &quot; & err.description End If

set RegKey = nothing set RegObj = nothing 



STATUS
This behavior is by design.



Steps to Reproduce Behavior
To reproduce this problem, try to add a key or value that has a UNC path as a name. For example: Set WshShell = CreateObject(&quot;Wscript.Shell&quot;) WshShell.RegWrite &quot;HKLM\SOFTWARE\MyKey\\\Servername\Sharename&quot;, &quot;Username&quot; Set WshShell = Nothing

