Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment.GetEnvironmentVariable("windir") strange behavior

The line is

if (!val.Contains(Environment.GetEnvironmentVariable("windir")))

Which seems to work just fine with a Windows 7(64-bit) system and will detect whether the string I'm looking at contains any version of c:\windows with any capitalization. But under XP, the same code does not seem to detect the changes in case. I have all the results printing to the console for debugging and some c:\WINDOWS are getting through, but when I execute

echo %windir%

in the Command window, I get C:\WINDOWS. Is there a way to rewrite my test so that it doesn't break compatibility with Win7 but will catch these differences with XP?

Thanks

like image 286
seanr8 Avatar asked Jul 21 '13 02:07

seanr8


1 Answers

I'd recommend using this instead to get the windows folder:

var windowsPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows)

And you can do this for case-insensitive comparison:

if (val.IndexOf(windowsPath, StringComparison.InvariantCultureIgnoreCase) < 0)
like image 102
p.s.w.g Avatar answered Oct 20 '22 00:10

p.s.w.g