henriquesirot
henriquesirot

Reputation: 115

C#: string concatenation not working

Today I came across a problem I had never had before. I'm trying to check if a file exists from a value in Windows registry. To be more specific, I'm getting the installation path from Flight Simulator and checking if there is a module installed.

I get the registry key just fine and it is converted to a string (from an object) but I don't seem to be able to concatenate the filename after it (before is just fine).

RegistryKey pRegKey = Registry.CurrentUser;
pRegKey = pRegKey.OpenSubKey(@"Software\Microsoft\Microsoft Games\Flight Simulator\10.0");
string fSPath = pRegKey.GetValue("AppPath").ToString(); // Receives "S:/Apps/FSX/"

If I show a messagebox like this it does not concatenate:

MessageBox.Show(fSPath + "Modules");

Upvotes: 1

Views: 1751

Answers (2)

Ria
Ria

Reputation: 10357

Try other overloaded methods of GetValue:

GetValue Method (String, Object)
GetValue Method (String, Object, RegistryValueOptions)

and use RegistryKey.GetValueKind method to get registry data type of the value associated with the specified name.

we have three registry data types for string:

String A null-terminated string.This value is equivalent to the Win32 API registry data type REG_SZ.

ExpandString A null-terminated string that contains unexpanded references to environment variables, such as %PATH%, that are expanded when the value is retrieved. This value is equivalent to the Win32 API registry data type REG_EXPAND_SZ.

MultiString An array of null-terminated strings, terminated by two null characters. This value is equivalent to the Win32 API registry data type REG_MULTI_SZ.

Upvotes: 3

CSharpie
CSharpie

Reputation: 9477

Add the following line:

string fSPath = fsPath.Replace("\0","");

before using messagebox.

Upvotes: 2

Related Questions