P
Primera
Could someone please review my method below. I'm trying to return the type
of the OS on the local machine using the below. My concern is that there
could be a flaw in the if statement and the return statement with variable
scope on osName. I originally had the return statement inside the foreach
block, but I got an error 'not all code paths return a value'. After moving
the return statement outside of the foreach block I get no compile time errors.
I'm still a bit weak in the area of variable scope. Thanks in advance.
public static string osType()
{
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT
* FROM Win32_OperatingSystem");
ManagementObjectCollection queryCollection = query.Get();
string osName = null;
foreach (ManagementObject mo in queryCollection)
{
string os = Convert.ToString(mo.Properties["Name"].Value);
int testname = os.IndexOf("XP");
if (testname != -1)
{
osName = "WinXP";
}
else
{
osName = "Win2k";
}
Console.WriteLine(os);
}
return osName;
}
of the OS on the local machine using the below. My concern is that there
could be a flaw in the if statement and the return statement with variable
scope on osName. I originally had the return statement inside the foreach
block, but I got an error 'not all code paths return a value'. After moving
the return statement outside of the foreach block I get no compile time errors.
I'm still a bit weak in the area of variable scope. Thanks in advance.
public static string osType()
{
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT
* FROM Win32_OperatingSystem");
ManagementObjectCollection queryCollection = query.Get();
string osName = null;
foreach (ManagementObject mo in queryCollection)
{
string os = Convert.ToString(mo.Properties["Name"].Value);
int testname = os.IndexOf("XP");
if (testname != -1)
{
osName = "WinXP";
}
else
{
osName = "Win2k";
}
Console.WriteLine(os);
}
return osName;
}