0

I ask a question to the user of my application with a MessageBox using the following code:

var site = Registry.GetValue("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Netlogon\\Parameters",
    "DynamicSiteName", "Unconnected") as string;
var selectedSite = (Options.GetUserSetting("Site", "SelectedSite"));

if () // Some checks go here
{
    DialogResult ret = MessageBox.Show(
        string.Format("The selected site is {0}. But you're in {1}...{2}Want to change it ?", selectedSite, site.Trim(), Environment.NewLine),
        "Stupid default selection...",
        MessageBoxButtons.YesNo,
        MessageBoxIcon.Question);
    // Some process goes here
}

The MessageBox is displayed with the following text:

The selected site is [SelectedSiteNameGoesHere]. But you're in [SiteGoesHere]

which is a bit disturbing because I don't understand why the string is splitted.

If I replace site with and hard-coded string, the message is displayed normally.

How can I retrieve the missing part?

Edit

Here's the output I have:

enter image description here

And here's the output I want:

enter image description here

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • You've got formatting that uses `Environment.NewLine`.. What did you want to happen? (Apologies for the invalid edit) – Sayse May 11 '15 at 07:21
  • 1
    Can't reproduce it here. What is is `site` and `selectedSite`? Here it shows `Want to change it ?` on the second line. – Patrick Hofman May 11 '15 at 07:22
  • Are there special characters in your string? – Patrick Hofman May 11 '15 at 07:27
  • @PatrickHofman both `site` and `selectedSite` are string, that's why I don't understand why I got this. No special char, the string `site` is `FR-LYON` – Thomas Ayoub May 11 '15 at 07:27
  • 1
    It might help to output the string to a variable first to see what value it has, Its going to be hard to reproduce without knowing exactly what kind of text (i.e length and any special chars) is included – Sayse May 11 '15 at 07:29
  • @Sayse printing the messagebox string in the console, I get the same result. Using `Console.WriteLine("---" + site.trim() + "---")` I get `---FR-LYON` as output – Thomas Ayoub May 11 '15 at 07:33
  • Again, its hard to see what could be causing the issue unless you have hidden/invalid characters in the string (i.e `\r\n` although this should be fine). Set a breakpoint and check the variables unescaped value – Sayse May 11 '15 at 07:40
  • @Sayse the raw string is `FR-LYON\0S`... You've got the point. But I don't know (yet) how to fix it – Thomas Ayoub May 11 '15 at 07:43
  • 1
    The best place would be wherever you set site in the first place to not have the `NUL` character, you could always try to remove it before doing messagebox but that won't help as much as correctly setting the string – Sayse May 11 '15 at 07:47
  • @Thomas: Take a look at [this answer](http://stackoverflow.com/a/5303390/993547) if you need to strip it. As Sayse set: fix the source! – Patrick Hofman May 11 '15 at 07:53
  • @Thomas: Is the issue resolved? – Patrick Hofman May 11 '15 at 09:48
  • @PatrickHofman yes, I split the site var using split('\0')[0] which is a bit dirty but makes the job – Thomas Ayoub May 11 '15 at 10:21

0 Answers0