C# 如何检查数字是否更大?

标签 c#

我正在为我在学校的类(class)开发一个程序,我正在设置自动更新。有没有办法让“else if (webClient.DownloadString("mylink").Contains("0.3.9"))”检查链接的包含以查看它是否超过或大于 0.3.9?

    public Form1()
    {
        InitializeComponent();
        WebClient webClient = new WebClient();  
        if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\updates\\Update-0.4.0.xml")) { }

        else if (webClient.DownloadString("mylink").Contains("0.3.9"))
        {
            if (MessageBox.Show("An Update is Avaliable, Would you like to download it?", "DesktopReborn Updater", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
            {
                webClient.DownloadFile("myupdate", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\DesktopReborn.exe");

                if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\updates\\Update-0.3.9.xml"))
                {
                    File.Copy(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\updates\\Update-0.3.9.xml", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\updates\\Update-0.4.0.xml", true);
                    File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\updates\\Update-0.3.9.xml");
                }
            }
        }
    }

最佳答案

您可以使用 Version 类来解析和比较这样的字符串,例如:

string thisVersion = "0.3.9";
string newVersion = "0.4.0";

if (Version.Parse(newVersion) > Version.Parse(thisVersion))
{
    Console.WriteLine($"{newVersion} is greater than {thisVersion}");
}

输出

enter image description here

关于C# 如何检查数字是否更大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53126933/

上一篇:C# VB.NET 转换

下一篇:C# 分组变量赋值

相关文章:

c# - 从 Sharepoint 流式传输 WCF

c# - 使用 yield 来推迟 Azure blob 存储调用 - c#

c# - 删除 XML 元素节点

c# - 使用 ConcurrentExclusiveSchedulerPair 作为异步 ReaderWriterLock 等价物

c# - 在一个解决方案中引用多个项目的资源文件

c# - 如何使用 asp.net 和 c# 流式传输 Excel 2007 或 Word 2007 文件

c# - 如何在不直接使用 ILifetimeScope 的情况下管理 WCF 服务内部的生命周期范围?

c# - 响应已开始 Blazor 中的 HttpContext.SignOutAsync() 方法调用引发异常

用于汇编样式十六进制数字的 C# 正则表达式

c# - 将 Xml 转换为字符串。每个标签换行?