C# registry read converts string

  • Thread starter Thread starter auldh
  • Start date Start date
A

auldh

i'm trying to read a registry key in both local and remote machines. for
example the HKLM\SYSTEM\CURRENTCONTROLSET\SERVICES\LANMANWORKSTATION there is
a key called "imagepath" this is a "reg_expand_sz" and it will look like:
"%systemroot%\system32\svchost.exe -k netsvcs".

when i run my code on either local or remote machine it will convert
"%systemroot%" to the local machine system drive, in my cast
"e:\system32\svchost.exe -k netsvcs". so now my output is incorrect.

here is a sample of my code: if (t ==
System.Type.GetType("System.String"))
{
string sval = (string)regvalue;
//Trace.WriteLine(String.Format("Value Name: {0}; Type:
{1}", valueName, t));
//Trace.WriteLine(String.Format("Value Data: {0}", sval));
sw.WriteLine(String.Format("\t\t{0} = (REG_SZ) {1}",
valueName, sval));
regexportreg_sz(sval, valueName, sw2);
}

when "string sval = (string)regvalue" reads that key it converts it. however
if you run the regedit export to text file it does not.

is there away to read the data to maintain its appearance?
 
auldh said:
i'm trying to read a registry key in both local and remote machines. for
example the HKLM\SYSTEM\CURRENTCONTROLSET\SERVICES\LANMANWORKSTATION there is
a key called "imagepath" this is a "reg_expand_sz" and it will look like:
"%systemroot%\system32\svchost.exe -k netsvcs".

when i run my code on either local or remote machine it will convert
"%systemroot%" to the local machine system drive, in my cast
"e:\system32\svchost.exe -k netsvcs". so now my output is incorrect.

[...]
is there away to read the data to maintain its appearance?

Yes.

Unfortunately, you haven't actually posted the code where you retrieve
the registry value. So we have no way to know whether your own code can
be modified to work.

But, if you're using the Microsoft.Win32.RegistryKey class, you can
simply use the GetValue() overload that allows you to pass the
RegistryValueOptions.DoNotExpandEnvironmentNames option.

Pete
 
Back
Top