C#:输入字符串的格式不正确,但一切正常

C#: Input string was not in a correct format but everything should work

程序应从 this file 中读取数字并将其转换为 double

除一台设备外,其他地方一切正常。在我朋友的笔记本电脑上,代码抛出 Input string was not in a correct format 异常。

同时,在转换前,我特地输出了一个要转换成double的字符串,这个字符串对应格式

可能是什么问题?

代码:

using System;
using System.Net;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            double newVersion = 0;
            try
            {
                string data = new WebClient().DownloadString("https://raw.githubusercontent.com/denisnumb/Keyboardpp/main/last_version");
                data = data.Replace(".", ",");
                Console.WriteLine(data);
                newVersion = Convert.ToDouble(data);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine($"{newVersion}");
            Console.ReadLine();
        }
    }
}

我设备上的输出:

0,92

0,92

朋友设备上的输出:

0,92

Input string was not in a correct format
0

我查看了您的代码并得到以下输入:

  • 使用 HttpClient 而不是 WebClient。
HttpClient client = new HttpClient();
var data = await client.GetStringAsync("https://raw.githubusercontent.com/denisnumb/Keyboardpp/main/last_version");
  • 使用 Tryparse 而不是转换,在这种情况下,您可以选择类型,如果您确实需要双精度类型或评论中提到的版本类型。
NumberFormatInfo formatWithComma = new NumberFormatInfo();
formatWithComma.NumberDecimalSeparator = ".";
double.TryParse(data, NumberStyles.Any, formatWithComma, out newVersion);

//newVersion type should be Version and not double
Version.TryParse(data, out newVersion);

如果你总结一下,这是我的建议,double.TryParse

static async Task Main(string[] args)
{
    double newVersion = 0;
    try
    {
        HttpClient client = new HttpClient();
        var data = await client.GetStringAsync("https://raw.githubusercontent.com/denisnumb/Keyboardpp/main/last_version");

        NumberFormatInfo formatWithComma = new NumberFormatInfo();
        formatWithComma.NumberDecimalSeparator = ".";
        var parsed = double.TryParse(data, NumberStyles.Any, formatWithComma, out newVersion);

        if (parsed)
        {
            Console.WriteLine(newVersion);
        }
        else
        {
            Console.WriteLine($"some thing went wrong {data}");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
    Console.WriteLine($"{newVersion}");
    Console.ReadLine();
}

或 Version.TryParse

static async Task Main(string[] args)
{
    Version newVersion = null;
    try
    {
        HttpClient client = new HttpClient();
        var data = await client.GetStringAsync("https://raw.githubusercontent.com/denisnumb/Keyboardpp/main/last_version");

        var parsed = Version.TryParse(data, out newVersion);

        if (parsed)
        {
            Console.WriteLine(newVersion);
        }
        else
        {
            Console.WriteLine($"some thing went wrong {data}");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
    Console.WriteLine($"{newVersion}");
    Console.ReadLine();
}