使用字符串c#检查版本

本文关键字:检查 版本 字符串 | 更新日期: 2023-09-27 18:03:31

编辑:

我想如何检查的简单示例:

我有一个包含:"0101"的string

我想检查string是否至少为0101或更高,而不将其转换为int,因为这会将我的string更改为:101。

编辑:

答案如下:

 private void VersionCheck(string version)
        {
            string firmwareVersion = version.Substring(0, 4);
            string biosVersion = version.Substring(4, 4);
            var isEqualOrHigherFirmwareVersion = string.Compare(firmwareVersion, Settings.MinimalFirmwareUnikon, CultureInfo.InvariantCulture, CompareOptions.None) >= 0;
        if (!isEqualOrHigherFirmwareVersion)
        {
            _serial.ClosePortConnection();
            DisplayLog("CLOCK: Firmware NOT OK : Unsupported version");
        MessageBox.Show("Unsupported clock Firmware!", "Firmware", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            DisplayLog("CLOCK: Firmware OK : v" + firmwareVersion.ToString());
        }
        var isEqualOrHigherBiosVersion = string.Compare(biosVersion,     `Settings.MinimalBiosUnikon, CultureInfo.InvariantCulture, CompareOptions.None) >= 0;`
        if (!isEqualOrHigherBiosVersion)
        {
            _serial.ClosePortConnection();
            DisplayLog("CLOCK: BIOS NOT OK : Unsupported version");
            MessageBox.Show("Unsupported clock BIOS version!", "BIOS", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            DisplayLog("CLOCK: BIOS OK : v" + biosVersion.ToString());
        }
    }

使用字符串c#检查版本

var input = "0102";
var supportedVersion = "0101";
var isGreaterOrHigher = string.Compare(input, supportedVersion, 
                    CultureInfo.InvariantCulture, CompareOptions.None) >= 0;

但是int.Parse/int.TryParse怎么了?