一个把文件转换成代码数组的小工具(源代码)

测试网址  http://silverlight.services.live.com/invoke/84388/Bin2Text/iframe.html

注意:大文件会很慢

  1. using System.IO;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. namespace Bin2Text
  5. {
  6.     public partial class Page : UserControl
  7.     {
  8.         private TextBox result = new TextBox();
  9.         public Page()
  10.         {
  11.             InitializeComponent();
  12.             this.LayoutRoot.Children.Add(this.result);
  13.             this.result.TextWrapping = TextWrapping.Wrap;
  14.             OpenFileDialog diag = new OpenFileDialog();
  15.             if (diag.ShowDialog() == true)
  16.             {
  17.                 FileStream stream = diag.File.OpenRead();
  18.                 long count = stream.Length - 1;
  19.                 this.result.Text += "byte[] result = new byte[]/n";
  20.                 this.result.Text += "{/n    ";
  21.                 for (int i = 0; i < count; i++)
  22.                 {
  23.                     this.result.Text += string.Format("0x{0:X2}, ", stream.ReadByte());
  24.                     if ((i + 1) % 8 == 0) this.result.Text += "/n    ";
  25.                 }
  26.                 this.result.Text += string.Format("0x{0:X2}/n", stream.ReadByte());
  27.                 this.result.Text += "};/n";
  28.                 stream.Close();
  29.                 this.result.SelectAll();
  30.             }
  31.         }
  32.     }
  33. }

猜你喜欢

转载自blog.csdn.net/KAMILLE/article/details/3373729