質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

10498閲覧

C# [Input string was not in a correct format.] の原因が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2022/05/17 11:17

編集2022/05/18 02:09

提示コードのコメント部内部のコードですが内部のコードで例外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

1namespace YoutubeDownloader 2{ 3 using System.Diagnostics; 4 using System; 5 using System.IO; 6 using System.Diagnostics; 7 8 9 public partial class Form1 : Form 10 { 11 ProcessStartInfo process = new ProcessStartInfo(); 12 Process pro = new Process(); 13 14 String? url = null; 15 String? format = null; 16 17 //指定した文字以降を削除 18 static public string GetRemoveRight(string str, string removeStr) 19 { 20 var length = str.IndexOf(removeStr); 21 if (length < 0) 22 { 23 return str; 24 } 25 26 return str.Substring(0, length); 27 } 28 29 //フォーマットを取得 30 private void SetFormat() 31 { 32 33 pro.StartInfo.Arguments = " -F " + url; 34 pro.Start(); 35 pro.WaitForExit(); 36 string results = pro.StandardOutput.ReadToEnd(); 37 StringReader sr = new StringReader(results); 38 39 if (results == "") 40 { 41 MessageBox.Show("URLが不正です。", "", MessageBoxButtons.OK, MessageBoxIcon.Error); 42 } 43 else 44 { 45 String? line; 46 int i = 0; 47 while ((line = sr.ReadLine()) != null) 48 { 49 Debug.WriteLine(line); 50 i++; 51 52 if (i > 3) 53 { 54 comboBox1.Items.Add(line); 55 } 56 } 57 } 58 } 59 60 //進行速度監視 61 static void Output_Handler(object sendingProcess, DataReceivedEventArgs outLine) 62 { 63 //Debug.WriteLine(outLine.Data); 64 String? progress = outLine.Data; 65 66 if(progress != null) 67 { 68 69 progress.Replace("[download] ", ""); 70 Debug.WriteLine(progress); 71 72 progress = GetRemoveRight(progress," "); 73 74 } 75 76 77 } 78 79 80 //ダウンロード 81 private void DownLoad() 82 { 83 pro.StartInfo.Arguments = " ytsearch:" + format + " " + url; 84 pro.Start(); 85 86 87 while (pro.HasExited == false) 88 { 89 String? progress = pro.StandardOutput.ReadLine(); 90 91 if (progress.Contains("[download]") == true) 92 { 93 String? p = progress.Replace("[download]", ""); 94 if (p != null) 95 { 96 progress = p; 97 } 98 99 progress = GetRemoveRight(progress, "%"); 100 progress = progress.Replace(" ", ""); 101 102 /////////////////////////////////////////////////////////////////// 103 //Debug.WriteLine(progress); 104 Debug.WriteLine(Convert.ToInt32(progress)); 105 Debug.WriteLine(Convert.ToDouble(progress)); 106 /////////////////////////////////////////////////////////////////// 107 } 108 } 109 //pro.BeginOutputReadLine(); 110 pro.WaitForExit(); 111 112 113 //リセット 114 url = null; 115 format = null; 116 textBox1.Text = null; 117 } 118 119 public Form1() 120 { 121 InitializeComponent(); 122 } 123 124 /*############################ 初期化 ############################*/ 125 private void Form1_Load(object sender, EventArgs e) 126 { 127 128 pro.StartInfo.FileName = "youtube-dl"; 129 pro.ErrorDataReceived += new DataReceivedEventHandler(Output_Handler); 130 pro.OutputDataReceived += new DataReceivedEventHandler(Output_Handler); 131 132 133 pro.StartInfo.RedirectStandardOutput = true; 134 pro.StartInfo.RedirectStandardError = true; 135 pro.StartInfo.CreateNoWindow = true; // コンソール・ウィンドウを開かない 136 pro.StartInfo.UseShellExecute = false; // シェル機能を使用しない 137 } 138 139 //フォーマットを選択したとき 140 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 141 { 142 if (comboBox1 != null) 143 { 144 format = GetRemoveRight(comboBox1.SelectedItem.ToString(), " "); 145 } 146 } 147 148 //ダウンロードボタンをクリック 149 private void button3_Click(object sender, EventArgs e) 150 { 151 if ((url != null) && (format != null)) 152 { 153 DownLoad(); 154 } 155 else 156 { 157 if (format == null) 158 { 159 MessageBox.Show("フォーマットが指定されていません。", "", MessageBoxButtons.OK, MessageBoxIcon.Information); 160 } 161 162 if (url == null) 163 { 164 MessageBox.Show("URLが指定されていません","", MessageBoxButtons.OK,MessageBoxIcon.Information); 165 } 166 } 167 168 } 169 170 //URLを入力してEnterキーを押下 171 private void textBox1_KeyDown(object sender, KeyEventArgs e) 172 { 173 if (e.KeyCode == Keys.Enter) 174 { 175 url = textBox1.Text; 176 url = GetRemoveRight(url, "&"); //プレイリストの場合一つにする 177 178 textBox1.ReadOnly = true; //入力不可 179 180 SetFormat(); //フォーマットを取得 181 182 textBox1.ReadOnly = false; //入力可能 183 184 } 185 } 186 187 188 189 190 } 191}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2022/05/17 11:38

開発環境を書きましょう。 コードのどの行で例外がスローされるのかと、例外の型やエラーメッセージを自分で編集しないでそのまま書いてください。
退会済みユーザー

退会済みユーザー

2022/05/18 01:51 編集

開発環境を書きましょう。 先にも書いたけど何で無視するの? 何か事情があるならともかく(ないですよね?)、こちらのお願いを無視するなら、こちらもあなたの質問は無視しますよ。
退会済みユーザー

退会済みユーザー

2022/05/18 01:51

すいません忘れてました。
Zuishin

2022/05/18 01:58 編集

しまった、こいつだった。取り下げ。
退会済みユーザー

退会済みユーザー

2022/05/18 01:56

Visual Studio のバージョン、.NET Framework なのか .NET Core/.NET のどっちなのかとそのバージョンも書いてください。
退会済みユーザー

退会済みユーザー

2022/05/18 02:00

Convert.ToInt32 メソッド (String) で FormatException がスローされるということは、引数の progress という文字列が Int32 型に変換するには不適切ということです。デバッガを使ってどうなっているか調べてください。デバッガの使い方が分からなければ、まずそれを勉強してください。
int32_t

2022/05/18 02:25

質問文にある「24.6」などは何を表示したものですか? それが Convert.ToInt32() に渡されているという意味でしょうか?
退会済みユーザー

退会済みユーザー

2022/05/18 02:58

int32_tさん そういう意味です。
int32_t

2022/05/18 03:22

そこまで調べてあるなら、「Convert.ToInt32(”24.6") で例外が発生する理由が知りたい」という質問にすると話が早かったですね。
guest

回答1

0

ベストアンサー

提示コードのコメント部内部のコードですが内部のコードで例外Input string was not in a correct format.が発生するのですがこれは何が原因なのでしょうか?

Convert.ToInt32 メソッド (String) で FormatException がスローされるということは、引数の progress という文字列が Int32 型に変換するには不適切ということです。

ToInt32(String)
https://docs.microsoft.com/ja-jp/dotnet/api/system.convert.toint32?view=net-6.0#system-convert-toint32(system-string)

デバッガを使ってどうなっているか調べてください。デバッガの使い方が分からなければ、まずそれを勉強してください。

最初に Visual Studio デバッガーを見る
https://docs.microsoft.com/ja-jp/visualstudio/debugger/debugger-feature-tour?view=vs-2022

超初心者でもわかるデバッグ方法
https://docs.microsoft.com/ja-jp/visualstudio/debugger/debugging-absolute-beginners?view=vs-2022&tabs=csharp

投稿2022/05/18 02:19

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問