提示コードのコメント部内部のコードですが内部のコードで例外Input string was not in a correct format.
が発生するのですがこれは何が原因なのでしょうか?
環境
OS Windows10
IDE visual sutudio 2022
プロジェクト windowsフォーム(C#) .NET6.0
参考サイト
1,https://www.remember-the-time.xyz/2020/12/c-sharp-input-string-was-not-in-a-correct-format.html
2,https://teratail.com/questions/300417
3,https://stackoverflow.com/questions/8321514/input-string-was-not-in-a-correct-format
確認したいこと
コンソール画面では空白を取りのぞいた数値のみの文字列なのですがなぜでしょうか?
出力画面
24.6 24.6 24.7 24.7 24.7 24.7 24.8
cs
namespace YoutubeDownloader { using System.Diagnostics; using System; using System.IO; using System.Diagnostics; public partial class Form1 : Form { ProcessStartInfo process = new ProcessStartInfo(); Process pro = new Process(); String? url = null; String? format = null; //指定した文字以降を削除 static public string GetRemoveRight(string str, string removeStr) { var length = str.IndexOf(removeStr); if (length < 0) { return str; } return str.Substring(0, length); } //フォーマットを取得 private void SetFormat() { pro.StartInfo.Arguments = " -F " + url; pro.Start(); pro.WaitForExit(); string results = pro.StandardOutput.ReadToEnd(); StringReader sr = new StringReader(results); if (results == "") { MessageBox.Show("URLが不正です。", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { String? line; int i = 0; while ((line = sr.ReadLine()) != null) { Debug.WriteLine(line); i++; if (i > 3) { comboBox1.Items.Add(line); } } } } //進行速度監視 static void Output_Handler(object sendingProcess, DataReceivedEventArgs outLine) { //Debug.WriteLine(outLine.Data); String? progress = outLine.Data; if(progress != null) { progress.Replace("[download] ", ""); Debug.WriteLine(progress); progress = GetRemoveRight(progress," "); } } //ダウンロード private void DownLoad() { pro.StartInfo.Arguments = " ytsearch:" + format + " " + url; pro.Start(); while (pro.HasExited == false) { String? progress = pro.StandardOutput.ReadLine(); if (progress.Contains("[download]") == true) { String? p = progress.Replace("[download]", ""); if (p != null) { progress = p; } progress = GetRemoveRight(progress, "%"); progress = progress.Replace(" ", ""); /////////////////////////////////////////////////////////////////// //Debug.WriteLine(progress); Debug.WriteLine(Convert.ToInt32(progress)); Debug.WriteLine(Convert.ToDouble(progress)); /////////////////////////////////////////////////////////////////// } } //pro.BeginOutputReadLine(); pro.WaitForExit(); //リセット url = null; format = null; textBox1.Text = null; } public Form1() { InitializeComponent(); } /*############################ 初期化 ############################*/ private void Form1_Load(object sender, EventArgs e) { pro.StartInfo.FileName = "youtube-dl"; pro.ErrorDataReceived += new DataReceivedEventHandler(Output_Handler); pro.OutputDataReceived += new DataReceivedEventHandler(Output_Handler); pro.StartInfo.RedirectStandardOutput = true; pro.StartInfo.RedirectStandardError = true; pro.StartInfo.CreateNoWindow = true; // コンソール・ウィンドウを開かない pro.StartInfo.UseShellExecute = false; // シェル機能を使用しない } //フォーマットを選択したとき private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1 != null) { format = GetRemoveRight(comboBox1.SelectedItem.ToString(), " "); } } //ダウンロードボタンをクリック private void button3_Click(object sender, EventArgs e) { if ((url != null) && (format != null)) { DownLoad(); } else { if (format == null) { MessageBox.Show("フォーマットが指定されていません。", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (url == null) { MessageBox.Show("URLが指定されていません","", MessageBoxButtons.OK,MessageBoxIcon.Information); } } } //URLを入力してEnterキーを押下 private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { url = textBox1.Text; url = GetRemoveRight(url, "&"); //プレイリストの場合一つにする textBox1.ReadOnly = true; //入力不可 SetFormat(); //フォーマットを取得 textBox1.ReadOnly = false; //入力可能 } } } }
開発環境を書きましょう。
コードのどの行で例外がスローされるのかと、例外の型やエラーメッセージを自分で編集しないでそのまま書いてください。
開発環境を書きましょう。
先にも書いたけど何で無視するの? 何か事情があるならともかく(ないですよね?)、こちらのお願いを無視するなら、こちらもあなたの質問は無視しますよ。
すいません忘れてました。
しまった、こいつだった。取り下げ。
Visual Studio のバージョン、.NET Framework なのか .NET Core/.NET のどっちなのかとそのバージョンも書いてください。
Convert.ToInt32 メソッド (String) で FormatException がスローされるということは、引数の progress という文字列が Int32 型に変換するには不適切ということです。デバッガを使ってどうなっているか調べてください。デバッガの使い方が分からなければ、まずそれを勉強してください。
質問文にある「24.6」などは何を表示したものですか?
それが Convert.ToInt32() に渡されているという意味でしょうか?
int32_tさん そういう意味です。
そこまで調べてあるなら、「Convert.ToInt32(”24.6") で例外が発生する理由が知りたい」という質問にすると話が早かったですね。
まだ回答がついていません
会員登録して回答してみよう