无法从双精度转换为字符串c#

时间:2020-02-27 02:45:04

标签: c#

我已经尝试过了,但是它给我一个错误“无法从双精度转换为字符串”,我想将变量ProgressBarMegabyte更改为以下等式。

ProgressBarMegabyte = (Int32.Parse(ProgressBarMegabyte) + Double.Parse(Convert.ToString(ProgressedMegabytes)));

我的整个密码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace SoftwareStoreClientDownloader
{
    public partial class OfflineInstaller : Form
    {
        public OfflineInstaller()
        {
            InitializeComponent();
            FormClosing += OfflineInstaller_FormClosing;
        }

        private void OfflineInstaller_FormClosing(object sender, FormClosingEventArgs e)
        {
            File.WriteAllText("C:\\Closed.txt", "true");
        }

        private void OfflineInstaller_Load(object sender, EventArgs e)
        {
            if (!Directory.Exists("C:\\Program Files\\SoftwareStoreOffline"))
            {
                Directory.CreateDirectory("C:\\Program Files\\SoftwareStoreOffline");
            }
        }

        private void BrowseButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog s = new SaveFileDialog();
            s.DefaultExt = "iso";
            s.AddExtension = false;
            s.CheckPathExists = true;
            s.ShowDialog();
            try
            {
                BrowseFileTextBox.Text = s.FileName;
            }
            catch (Exception dew)
            {

            }
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            var i = 2;
            button1.Enabled = false;
            button1.Text = "Initializing";
            await Initialize();
            button1.Text = "Downloading";
            TotalProgressBar.Maximum = Int32.Parse(Parts);
            if (Int32.Parse(Parts) > 99)
            {
                await Download(
                    "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreOffline" + Database +
                    "/master/SoftwareStore" + Version + "Setup-Files.part001.exe",
                    MainDirectory + "\\" + "Setup-Files.part001.exe");
            }
            else
            {
                await Download(
                    "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreOffline" + Database +
                    "/master/SoftwareStore" + Version + "Setup-Files.part01.exe",
                    MainDirectory + "\\" + "Setup-Files.part01.exe");
            }
            var TotalProgressBytes = ((Int32.Parse(TotalBytes) / 1024d / 1024d) * Int32.Parse(Parts));
            var TotalMegabytes = TotalProgressBytes;
            var ProgressedMegabytes = (Int32.Parse(TotalBytes) / 1024d / 1024d);
            var ProgressBarMegabyte = string.Empty;
            ProgressBarMegabyte = ProgressedMegabytes.ToString();
            while (i < Int32.Parse(Parts) + 1)
            {
                if (Int32.Parse(Parts) < 100)
                {
                    if (i < 10)
                    {
                        await Download(
                            "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreOffline" + Database +
                            "/master/SoftwareStore" + Version + "Setup-Files.part0" + i + ".rar",
                            MainDirectory + "\\" + "Setup-Files.part0" + i + ".rar");
                    }
                    else
                    {
                        await Download(
                            "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreOffline" + Database +
                            "/master/SoftwareStore" + Version + "Setup-Files.part" + i + ".rar",
                            MainDirectory + "\\" + "Setup-Files.part" + i + ".rar");
                    }
                }
                else if (Int32.Parse(Parts) > 99)
                {
                    if (i < 10 && i < 100)
                    {
                        await Download(
                            "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreOffline" + Database +
                            "/master/SoftwareStore" + Version + "Setup-Files.part00" + i + ".rar",
                            MainDirectory + "\\" + "Setup-Files.part00" + i + ".rar");
                    }
                    else if(i > 10 && i < 100)
                    {
                        await Download(
                            "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreOffline" + Database +
                            "/master/SoftwareStore" + Version + "Setup-Files.part0" + i + ".rar",
                            MainDirectory + "\\" + "Setup-Files.part0" + i + ".rar");
                    }
                    else if (i > 99)
                    {
                        await Download(
                            "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreOffline" + Database +
                            "/master/SoftwareStore" + Version + "Setup-Files.part" + i + ".rar",
                            MainDirectory + "\\" + "Setup-Files.part" + i + ".rar");
                    }
                }
                else if (Int32.Parse(Parts) < 10)
                {
                    await Download(
                        "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreOffline" + Database +
                        "/master/SoftwareStore" + Version + "Setup-Files.part" + i + ".rar",
                        MainDirectory + "\\" + "Setup-Files.part" + i + ".rar");
                }

                DownloadProgressLabel.Text = "Status: (" + i + "/" + Parts + ")";
                if (i < TotalProgressBar.Maximum)
                {
                    TotalProgressBar.Value = i;
                }

                if (first == false)
                {
                    TotalProgressLabel.Text = "(" + ProgressBarMegabyte + " MB / " + TotalMegabytes + " MB)";
                    first = true;
                }
                else
                {
                    TotalProgressLabel.Text = "(" + ProgressBarMegabyte + " MB / " + TotalMegabytes + " MB)";
                }

                int Equation = Convert.ToInt32(Int32.Parse(ProgressBarMegabyte) +
                                       Double.Parse(Convert.ToString(ProgressedMegabytes)));
                ProgressBarMegabyte = Equation.ToString();

                i++;
            }

            if (Int32.Parse(Parts) > 99)
            {
                await Task.Factory.StartNew(() =>
                {
                    Process.Start(@"C:\Program Files\SoftwareStoreOffline\Setup-Files.part001.exe").WaitForExit();
                });
            }
            else if (Int32.Parse(Parts) < 100)
            {
                await Task.Factory.StartNew(() =>
                {
                    Process.Start(@"C:\Program Files\SoftwareStoreOffline\Setup-Files.part01.exe");
                });
            }
            else if(Int32.Parse(Parts) < 10)
            {
                await Task.Factory.StartNew(() =>
                {
                    Process.Start(@"C:\Program Files\SoftwareStoreOffline\Setup-Files.part1.exe");
                });
            }
        }

        private bool first = false;
        private string MainDirectory = "C:\\Program Files\\SoftwareStoreOffline";
        private string TotalBytes = string.Empty;
        private async Task Download(string link, string Filename)
        {
            using (var client = new WebClient())
            {
                client.DownloadProgressChanged += Client_DownloadProgressChanged;
                client.DownloadFileCompleted += Client_DownloadFileCompleted;
                client.DownloadFileAsync(new Uri(link), Filename);
                while (client.IsBusy)
                {
                    await Task.Delay(10);
                }
            }
        }

        private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {

            }
            else if (e.Error != null)
            {

            }
            else
            {

            }
        }

        private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            TotalBytes = e.TotalBytesToReceive.ToString();
            DownloadProgressBar.Value = e.ProgressPercentage;
        }

        private string Parts = string.Empty;
        private string Database = string.Empty;
        private string Version = string.Empty;
        private async Task Initialize()
        {
            try
            {
                await Download(
                    "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreClientDownloader/master/OfflineDatabaseNumber.txt",
                    MainDirectory + "\\OfflineDatabaseNumber.txt");
                await Download(
                    "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreClientDownloader/master/OfflineParts.txt",
                    MainDirectory + "\\OfflineParts.txt");
                await Download(
                    "https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreClientDownloader/master/OfflineVersion.txt",
                    MainDirectory + "\\OfflineVersion.txt");
                Parts = File.ReadLines(MainDirectory + "\\OfflineParts.txt").First();
                Database = File.ReadLines(MainDirectory + "\\OfflineDatabaseNumber.txt").First();
                Version = File.ReadLines(MainDirectory + "\\OfflineVersion.txt").First();
            }
            catch (Exception dew)
            {
                Console.WriteLine(dew);
                throw;
            }
        }
    }
}

我正在尝试使用该方程式,因为我想解决进度栏,最终我弄清楚了。我使用了int

4 个答案:

答案 0 :(得分:2)

代替使用Convert.ToString()

您可以简单地做到:ProgressedMegabytes.ToString()

答案 1 :(得分:1)

您的问题出在不同的类型上

  • ProgressBarMegabyte是一个字符串

  • ProgressedMigabytes是您所说的两倍。

那么您的分配人是:

sting = int + double

因此您出错。要解决此问题,只需将结果转换为字符串即可:

double ProgressedMegabytes = 1.5;
string ProgressBarMegabyte = "83345603 .....................";

ProgressBarMegabyte = (Int32.Parse(ProgressBarMegabyte.TrimEnd('.')) + ProgressedMegabytes).ToString();

Console.WriteLine(ProgressBarMegabyte); // 83345604.5

请注意ProgressBarMegabyte的值-您正在假设它是一个数字!

答案 2 :(得分:0)

这不是您问题的答案-我只是想发布重构的代码版本,以帮助您前进。

private async void button1_Click(object sender, EventArgs e)
{
    var i = 2;
    await Initialize();

    var name = Parts > 99 ? "part001" : "part01";
    await Download($"https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreOffline{Database}/master/SoftwareStore{Version}Setup-Files.{name}.exe", $"{MainDirectory}\\Setup-Files.{name}.exe");

    var format = new string('0', Parts.ToString().Length)
    while (i < Parts + 1)
    {
        await Download($"https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreOffline{Database}/master/SoftwareStore{Version}Setup-Files.part{i.ToString(format)}.rar", $"{MainDirectory}\\Setup-Files.{i.ToString(format)}.rar");
        i++;
    }

    await Task.Factory.StartNew(() =>
    {
        Process.Start($@"C:\Program Files\SoftwareStoreOffline\Setup-Files.part{1.ToString(format)}.exe").WaitForExit();
    });

}

private string MainDirectory = "C:\\Program Files\\SoftwareStoreOffline";
private async Task Download(string link, string Filename)
{
    using (var client = new WebClient())
    {
        await client.DownloadFileTaskAsync(link, Filename);
    }
}

private int Parts;
private string Database = string.Empty;
private string Version = string.Empty;
private async Task Initialize()
{
    try
    {
        await Download("https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreClientDownloader/master/OfflineDatabaseNumber.txt", MainDirectory + "\\OfflineDatabaseNumber.txt");
        await Download("https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreClientDownloader/master/OfflineParts.txt", MainDirectory + "\\OfflineParts.txt");
        await Download("https://raw.githubusercontent.com/CNTowerGUN/SoftwareStoreClientDownloader/master/OfflineVersion.txt", MainDirectory + "\\OfflineVersion.txt");
        Parts = int.Parse(File.ReadLines(MainDirectory + "\\OfflineParts.txt").First());
        Database = File.ReadLines(MainDirectory + "\\OfflineDatabaseNumber.txt").First();
        Version = File.ReadLines(MainDirectory + "\\OfflineVersion.txt").First();
    }
    catch (Exception dew)
    {
        Console.WriteLine(dew);
        throw;
    }
}

答案 3 :(得分:-2)

我自己用便宜的方法解决了这个问题:

int Equation = Convert.ToInt32(Int32.Parse(ProgressBarMegabyte)
    + Double.Parse(Convert.ToString(ProgressedMegabytes)));                
ProgressBarMegabyte = Equation.ToString();