Home > Back-end >  C#: Input string was not in a correct format but everything should work
C#: Input string was not in a correct format but everything should work

Time:05-25

The program should read the number from this file and convert it to double.

Everything works fine everywhere except for one device. On my friend's laptop, the code throws an Input string was not in a correct format exception.

At the same time, before converting, I specifically output a string that should be converted to double and this string corresponds to the format.

What could be the problem?

Code:

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();
        }
    }
}

Output on my device:

0,92

0,92

Output on a friend's device:

0,92

Input string was not in a correct format
0

CodePudding user response:

I have looked at your code and have the following input:

  • Use HttpClient instead of WebClient.
HttpClient client = new HttpClient();
var data = await client.GetStringAsync("https://raw.githubusercontent.com/denisnumb/Keyboardpp/main/last_version");
  • Use Tryparse instead of a convert, in this case, you can choose the type, if you really need it in double or as mentioned in the comment as Version type.
NumberFormatInfo formatWithComma = new NumberFormatInfo();
formatWithComma.NumberDecimalSeparator = ".";
double.TryParse(data, NumberStyles.Any, formatWithComma, out newVersion);

Or

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

If you wrap it up, here is my suggestion, with 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();
}

Or with 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();
}
  •  Tags:  
  • c#
  • Related