###前提・実現したいこと
C#で16進ダンププログラムを作成しています。
###発生している問題・エラーメッセージ
テキストファイルが開けない エラーメッセージ System.InvalidCastException:型'System.Byte[]'のオブジェクトを型'System.lConvertible'にキャストできません。 場所System.Convert.ToChar(Object value) 場所fileDump.FormFileDump.fileRead()
###該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10using System.IO; 11 12namespace fileDump 13{ 14 public partial class FormFileDump : Form 15 { 16 /// <summary> 17 /// ページ 18 /// </summary> 19 private int page; 20 21 /// <summary> 22 /// 読み取る最大のbyte数 23 /// 256byte 24 /// </summary> 25 private const int MAX = 256; 26 27 /// <summary> 28 /// 1行に表示する最大桁数 29 /// 16個 30 /// </summary> 31 private const int DISPLAY_MAX = 16; 32 33 /// <summary> 34 /// 総ブロック数 35 /// </summary> 36 private double totalBlock; 37 38 /// <summary> 39 /// ファイルのパス 40 /// </summary> 41 private FileStream filePass; 42 43 /// <summary> 44 /// C#研修問題2 ファイルダンプフォームのコンストラクタ 45 /// </summary> 46 public FormFileDump() 47 { 48 InitializeComponent(); 49 } 50 51 /// <summary> 52 /// ファイルダンプフォームのイベント 53 /// 現在の日付を表示 54 /// </summary> 55 /// <param name="sender">使用しない</param> 56 /// <param name="e">使用しない</param> 57 private void FormFileDump_Load(object sender, EventArgs e) 58 { 59 textBoxDate.Text = DateTime.Today.ToShortDateString();//現在の日付を得る 60 } 61 62 /// <summary> 63 /// ファイルを開くボタンのクリックイベント 64 /// 標準ダイアログを開いてユーザーにファイルを選択させる 65 /// </summary> 66 /// <param name="sender">使用しない</param> 67 /// <param name="e">使用しない</param> 68 private void buttonOpenFile_Click(object sender, EventArgs e) 69 { 70 //標準ダイアログ 71 OpenFileDialog log = new OpenFileDialog(); 72 log.FileName = "default.txt"; 73 log.InitialDirectory = ""; 74 log.FilterIndex = 2; 75 log.Title = "開くファイルを選択してください。"; 76 log.RestoreDirectory = true; 77 log.CheckFileExists = false; 78 79 if (log.ShowDialog() == DialogResult.Cancel) 80 { 81 return; 82 } 83 else 84 { 85 //既にファイルを選択してた時 86 if (textBoxFileName.Text != "") 87 { 88 filePass.Close();//ファイルを閉じる 89 } 90 91 try 92 { 93 textBoxFileName.Text = log.FileName; 94 filePass = File.Open(log.FileName, FileMode.Open);//ファイルを開く 95 long fileSize = filePass.Length;//ファイルサイズを得る 96 double total = (double)fileSize / MAX;//総ブロック数を求める 97 totalBlock = Math.Ceiling(total);//総ブロック数の切り上げ 98 page = 1; 99 fileRead(); 100 } 101 catch 102 { 103 MessageBox.Show("ファイルを開くのに失敗しました。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); 104 textBoxFileName.Text = ""; 105 } 106 } 107 } 108 109 /// <summary> 110 /// 戻るボタンのクリックイベント 111 /// 1ページ分前にシークさせる 112 /// </summary> 113 /// <param name="sender">使用しない</param> 114 /// <param name="e">使用しない</param> 115 private void buttonBack_Click(object sender, EventArgs e) 116 { 117 if (textBoxFileName.Text == "") 118 { 119 MessageBox.Show("ファイルが指定されていません。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error); 120 } 121 else 122 { 123 if (page == 1) 124 { 125 MessageBox.Show("移動先のページがありません。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); 126 } 127 else 128 { 129 filePass.Seek(-MAX, SeekOrigin.Current);//シーク 130 page--; 131 fileRead(); 132 } 133 } 134 } 135 136 /// <summary> 137 /// 次へボタンのクリックイベント 138 /// 1ページ分次へシークさせる 139 /// </summary> 140 /// <param name="sender">使用しない</param> 141 /// <param name="e">使用しない</param> 142 private void buttonNext_Click(object sender, EventArgs e) 143 { 144 if (textBoxFileName.Text == "") 145 { 146 MessageBox.Show("ファイルが指定されていません。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error); 147 } 148 else 149 { 150 if (page == totalBlock) 151 { 152 MessageBox.Show("移動先のページがありません。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); 153 } 154 else 155 { 156 filePass.Seek(MAX, SeekOrigin.Current);//シーク 157 page++; 158 fileRead(); 159 } 160 } 161 } 162 163 /// <summary> 164 /// 終了ボタンのクリックイベント 165 /// アプリケーションを終了させる 166 /// </summary> 167 /// <param name="sender">使用しない</param> 168 /// <param name="e">使用しない</param> 169 private void buttonEnd_Click(object sender, EventArgs e) 170 { 171 this.Close(); 172 } 173 174 /// <summary> 175 /// ファイル読み込みメソッド 176 /// アドレス,Hex,Ascを表示する 177 /// </summary> 178 private void fileRead() 179 { 180 int addr;//アドレス 181 int addr_offset;//アドレスのスタート位置 182 int output;//出力数 183 int hex = 0;//16進カウンタ 184 int asc = 0;//文字カウンタ 185 int readSize = 0;//Readメソッドの戻り値 186 187 textBoxFileData.Text = "Addr Hex Ascⅱ\r\n"; 188 189 byte[] data = new byte[MAX]; 190 191 try 192 { 193 readSize = filePass.Read(data, 0, MAX); 194 } 195 catch 196 { 197 MessageBox.Show("ファイルの読み込みに失敗しました。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); 198 } 199 200 addr = 0; 201 addr_offset = (MAX * (page -1)); 202 while (DISPLAY_MAX > addr) 203 { 204 textBoxFileData.Text = String.Format("{0:X4}", addr_offset + (addr * DISPLAY_MAX)); 205 206 for (output = 0; output < DISPLAY_MAX; output++) 207 { 208 if (readSize > hex) 209 { 210 textBoxFileData.Text = String.Format("{0 :X2}", data[hex]); 211 } 212 else 213 { 214 textBoxFileData.Text = " "; 215 } 216 217 hex++; 218 } 219 220 for (output = 0; output < DISPLAY_MAX; output++) 221 { 222 char key = Convert.ToChar(data); 223 224 if (readSize > asc) 225 { 226 if ((key >= 0x20) && (key <= 0x7e)) 227 { 228 textBoxFileData.Text = String.Format("{0}", data[asc]); 229 } 230 else 231 { 232 textBoxFileData.Text = "$"; 233 } 234 } 235 else 236 { 237 textBoxFileData.Text = " "; 238 } 239 240 asc++; 241 } 242 243 addr++; 244 textBoxFileData.Text = "\r\n"; 245 } 246 } 247 } 248} 249
###試したこと
ネットで調べながら作成していたのですが行き詰ってしまったのでこちらで質問させていただいた次第です。
###補足情報(言語/FW/ツール等のバージョンなど)
Visual Studio 2012を使っています。
どんな現象が起きていますか? エラーメッセージなどは出ていますか? 開こうとするファイルのパーミッションはどうなっていますか? もっと、詳しく説明してください。
標準ダイアログでaaa.txtという適当なテキストファイルを選択したらcatchのエラーメッセージを表示してます。ファイルのパーミッションはデフォルトのままです。Windows10です。
catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); } のように catchで例外を捕捉して、その内容を表示させると例外が起きた原因が判ります。原因が判れば、対策を考えることができます。
catch は2か所ありますが 「テキストファイルが開けない」とおっしゃっているので「"ファイルを開くのに失敗しました。"」の箇所でしょうね? catch を catch (Exception e) で受けて {} 内で MessageBox.Show(e.ToString()); とすれば詳しいエラーの内容がわかるかと思います。
そうです!どうやってメッセージボックスで表示させるのかと思ってました。ToStringを使えばよかったんですねありがとうございます!
fileRead()の中のConvert.ToCharのところでエラーが出てました
後は、メッセージボックスの内容を記述すれば、回答できるかと思います。(知られたくない箇所は、伏字にすることをお忘れなく)
ありがとうございます!載せておきます。
回答1件
あなたの回答
tips
プレビュー