Form1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:5k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8. using System.Text;
  9. namespace Wrox.ProCSharp.FilesAndRegistry
  10. {
  11. /// <summary>
  12. /// Summary description for Form1.
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. private OpenFileDialog chooseOpenFileDialog = new OpenFileDialog();
  17. private string chosenFile;
  18. private System.Windows.Forms.MainMenu mainMenu1;
  19. private System.Windows.Forms.MenuItem menuItem1;
  20. private System.Windows.Forms.TextBox textBoxContents;
  21. private System.Windows.Forms.MenuItem menuFileOpen;
  22. /// <summary>
  23. /// Required designer variable.
  24. /// </summary>
  25. private System.ComponentModel.Container components = null;
  26. public Form1()
  27. {
  28. //
  29. // Required for Windows Form Designer support
  30. //
  31. InitializeComponent();
  32. menuFileOpen.Click += new EventHandler(OnFileOpen);
  33. chooseOpenFileDialog.FileOk += new 
  34. CancelEventHandler(OnOpenFileDialogOK);
  35. }
  36. void OnFileOpen(object Sender, EventArgs e)
  37. {
  38. chooseOpenFileDialog.ShowDialog();
  39. }
  40. void OnOpenFileDialogOK(object Sender, CancelEventArgs e)
  41. {
  42. chosenFile = chooseOpenFileDialog.FileName;
  43. this.Text = Path.GetFileName(chosenFile);
  44. DisplayFile();
  45. }
  46. void DisplayFile()
  47. {
  48. int nCols = 16;
  49. FileStream inStream = new FileStream(chosenFile, FileMode.Open, 
  50. FileAccess.Read);
  51. long nBytesToRead = inStream.Length;
  52. if (nBytesToRead > 65536/4) 
  53. nBytesToRead = 65536/4;
  54. int nLines = (int)(nBytesToRead/nCols) + 1;
  55. string [] lines = new string[nLines];
  56. int nBytesRead = 0;
  57. for (int i=0 ; i<nLines ; i++)
  58. {
  59. StringBuilder nextLine = new StringBuilder();
  60. nextLine.Capacity = 4*nCols;
  61. for (int j = 0 ; j<nCols ; j++)
  62. {
  63. int nextByte = inStream.ReadByte();
  64. nBytesRead++;
  65. if (nextByte < 0 || nBytesRead > 65536)
  66. break;
  67. char nextChar = (char)nextByte;
  68. if (nextChar < 16)
  69. nextLine.Append(" x0" + string.Format("{0,1:X}", 
  70. (int)nextChar));
  71. else if 
  72. (char.IsLetterOrDigit(nextChar) || 
  73. char.IsPunctuation(nextChar))
  74. nextLine.Append("  " + nextChar + " ");
  75. else
  76. nextLine.Append(" x" + string.Format("{0,2:X}", 
  77. (int)nextChar));
  78. }
  79. lines[i] = nextLine.ToString();
  80. }
  81. inStream.Close();
  82. this.textBoxContents.Lines = lines;
  83. }
  84. /// <summary>
  85. /// Clean up any resources being used.
  86. /// </summary>
  87. protected override void Dispose( bool disposing )
  88. {
  89. if( disposing )
  90. {
  91. if (components != null) 
  92. {
  93. components.Dispose();
  94. }
  95. }
  96. base.Dispose( disposing );
  97. }
  98. #region Windows Form Designer generated code
  99. /// <summary>
  100. /// Required method for Designer support - do not modify
  101. /// the contents of this method with the code editor.
  102. /// </summary>
  103. private void InitializeComponent()
  104. {
  105. this.textBoxContents = new System.Windows.Forms.TextBox();
  106. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  107. this.menuItem1 = new System.Windows.Forms.MenuItem();
  108. this.menuFileOpen = new System.Windows.Forms.MenuItem();
  109. this.SuspendLayout();
  110. // 
  111. // textBoxContents
  112. // 
  113. this.textBoxContents.Dock = System.Windows.Forms.DockStyle.Fill;
  114. this.textBoxContents.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  115. this.textBoxContents.Multiline = true;
  116. this.textBoxContents.Name = "textBoxContents";
  117. this.textBoxContents.ScrollBars = System.Windows.Forms.ScrollBars.Both;
  118. this.textBoxContents.Size = new System.Drawing.Size(292, 268);
  119. this.textBoxContents.TabIndex = 0;
  120. this.textBoxContents.Text = "";
  121. // 
  122. // mainMenu1
  123. // 
  124. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  125.   this.menuItem1});
  126. // 
  127. // menuItem1
  128. // 
  129. this.menuItem1.Index = 0;
  130. this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  131.   this.menuFileOpen});
  132. this.menuItem1.Text = "&File";
  133. // 
  134. // menuFileOpen
  135. // 
  136. this.menuFileOpen.Index = 0;
  137. this.menuFileOpen.Text = "&Open";
  138. // 
  139. // Form1
  140. // 
  141. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  142. this.ClientSize = new System.Drawing.Size(292, 268);
  143. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  144.   this.textBoxContents});
  145. this.Menu = this.mainMenu1;
  146. this.Name = "Form1";
  147. this.Text = "BinaryFileReader Sample";
  148. this.Load += new System.EventHandler(this.Form1_Load);
  149. this.ResumeLayout(false);
  150. }
  151. #endregion
  152. /// <summary>
  153. /// The main entry point for the application.
  154. /// </summary>
  155. [STAThread]
  156. static void Main() 
  157. {
  158. Application.Run(new Form1());
  159. }
  160. private void Form1_Load(object sender, System.EventArgs e)
  161. {
  162. }
  163. }
  164. }