Ankush Madankar Ankush Madankar - 20 days ago 6
C# Question

How to parse OS version name into Version class

I am having OS version string like:

Microsoft Windows 7 Professional 6.1.7601 Service Pack 1


Microsoft Windows NT 5.1.2600 Service Pack 3


Microsoft Windows Storage Server 2008 R2 Essentials 6.1.7601 Service Pack 1


How could I parse those string values into
Version
class?

Answer

You can split your string with a space a get the string version:

        string Os = "Microsoft Windows 7 Professional 6.1.7601 Service Pack 1";
        string[] splitOs = Os.Split(null);

        foreach (var item in splitOs)
        {
            if (item.Contains("."))
            {
                Version v1 = new Version(item)
            }
        }