hottabxp
@hottabxp
Миллиардер. Честно, 100 пистонов!

Как сравнивать версии моей программы на c#?

Например, сейчас версия моей программы - 1.3.99 Потом я добавил фичи, и версия стала 1.5.63. Потом программа проверяет( скачивает xml файл с сервера) наличие новой версии. Как сравнить 1.3.99 и 1.5.63?
Нужно именно 3 группы цифр.
  • Вопрос задан
  • 161 просмотр
Решения вопроса 1
hePPer
@hePPer
Version v1 = new Version("1.1.2");
Version v2 = new Version("2.1.1");
if(v2>v1){
bla bla
}
Ответ написан
Пригласить эксперта
Ответы на вопрос 2
VoidVolker
@VoidVolker
/// <summary>
/// Version
/// </summary>
public class Version : IComparable<Version>
{
    /// <summary>
    /// Major version
    /// </summary>
    public readonly int Major = 0;
    /// <summary>
    /// Minor version
    /// </summary>
    public readonly int Minor = 0;
    /// <summary>
    /// Build number
    /// </summary>
    public readonly int Build = 0;
    /// <summary>
    /// Revision
    /// </summary>
    public readonly int Revision = 0;

    private readonly int hash;

    /// <summary>
    /// Version
    /// </summary>
    /// <param name="version"></param>
    public Version(string version)
    {
        string[] versions = version.Split('.');

        if (versions == null)
        {
            throw new Exception("Can't parse version from string: " + version);
        }

        if (versions.Length != 4)
        {
            throw new Exception("Can't parse version from string — wrong count of numbers: " + version);
        }

        if (int.TryParse(versions[0], out int major))
        {
            Major = major;
        }
        else
        {
            throw new Exception("Can't parse major version from string: " + versions[0]);
        }

        if (int.TryParse(versions[1], out int minor))
        {
            Minor = minor;
        }
        else
        {
            throw new Exception("Can't parse minor version from string: " + versions[1]);
        }

        if (int.TryParse(versions[2], out int build))
        {
            Build = build;
        }
        else
        {
            throw new Exception("Can't parse build version from string: " + versions[2]);
        }

        if (int.TryParse(versions[3], out int revisison))
        {
            Revision = revisison;
        }
        else
        {
            throw new Exception("Can't parse revisison version from string: " + versions[3]);
        }

        hash = ToString().GetHashCode();
    }

    /// <summary>
    /// Versions compare
    /// </summary>
    /// <param name="other"></param>
    /// <returns></returns>
    public int CompareTo(Version other)
    {
        if (Major > other.Major) { return 1; }
        if (Major < other.Major) { return -1; }
        // Major == other.Major
        if (Minor > other.Minor) { return 1; }
        if (Minor < other.Minor) { return -1; }
        // Minor == other.Minor
        if (Build > other.Build) { return 1; }
        if (Build < other.Build) { return -1; }
        // Build == other.Build
        if (Revision > other.Revision) { return 1; }
        if (Revision < other.Revision) { return -1; }
        // Revision == other.Revision
        return 0;
    }

    /// <summary>
    /// Bigger?
    /// </summary>
    /// <param name="left"></param>
    /// <param name="right"></param>
    /// <returns></returns>
    public static bool operator >(Version left, Version right)
    {
        return left.CompareTo(right) == 1;
    }

    /// <summary>
    /// Less?
    /// </summary>
    /// <param name="left"></param>
    /// <param name="right"></param>
    /// <returns></returns>
    public static bool operator <(Version left, Version right)
    {
        return left.CompareTo(right) == -1;
    }

    /// <summary>
    /// Bigger or equal?
    /// </summary>
    /// <param name="left"></param>
    /// <param name="right"></param>
    /// <returns></returns>
    public static bool operator >=(Version left, Version right)
    {
        return left.CompareTo(right) >= 0;
    }

    /// <summary>
    /// Less or equal?
    /// </summary>
    /// <param name="left"></param>
    /// <param name="right"></param>
    /// <returns></returns>
    public static bool operator <=(Version left, Version right)
    {
        return left.CompareTo(right) <= 0;
    }

    /// <summary>
    /// Convert version to string
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return string.Format("{0}.{1}.{2}.{3}", Major, Minor, Build, Revision);
    }

    /// <summary>
    /// Get hash
    /// </summary>
    /// <returns></returns>
    public override int GetHashCode()
    {
        return hash;
    }
}
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы