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.Collections.Specialized;
  9. using System.Text;
  10. namespace Wrox.ProCSharp.FilesAndRegistry
  11. {
  12. /// <summary>
  13. /// Summary description for Form1.
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private OpenFileDialog chooseOpenFileDialog = new OpenFileDialog();
  18. private string chosenFile;
  19. private System.Windows.Forms.MainMenu mainMenu1;
  20. private System.Windows.Forms.MenuItem menuItem1;
  21. private System.Windows.Forms.TextBox textBoxContents;
  22. private System.Windows.Forms.MenuItem menuFileOpen;
  23. private System.Windows.Forms.MenuItem menuFileSave;
  24. /// <summary>
  25. /// Required designer variable.
  26. /// </summary>
  27. private System.ComponentModel.Container components = null;
  28. public Form1()
  29. {
  30. //
  31. // Required for Windows Form Designer support
  32. //
  33. InitializeComponent();
  34. menuFileOpen.Click += new EventHandler(OnFileOpen);
  35. chooseOpenFileDialog.FileOk += new 
  36. CancelEventHandler(OnOpenFileDialogOK);
  37. }
  38. void OnFileOpen(object Sender, EventArgs e)
  39. {
  40. chooseOpenFileDialog.ShowDialog();
  41. }
  42. void OnFileSave(object Sender, EventArgs e)
  43. {
  44. SaveFile();
  45. }
  46. void OnOpenFileDialogOK(object Sender, CancelEventArgs e)
  47. {
  48. chosenFile = chooseOpenFileDialog.FileName;
  49. this.Text = Path.GetFileName(chosenFile);
  50. DisplayFile();
  51. }
  52. /// <summary>
  53. /// Clean up any resources being used.
  54. /// </summary>
  55. protected override void Dispose( bool disposing )
  56. {
  57. if( disposing )
  58. {
  59. if (components != null) 
  60. {
  61. components.Dispose();
  62. }
  63. }
  64. base.Dispose( disposing );
  65. }
  66. void SaveFile()
  67. {
  68. StreamWriter sw = new StreamWriter(chosenFile, false, 
  69. Encoding.Unicode);
  70. foreach (string line in textBoxContents.Lines)
  71. sw.WriteLine(line);
  72. sw.Close();
  73. }
  74. void DisplayFile()
  75. {
  76. StringCollection linesCollection = ReadFileIntoStringCollection();
  77. string [] linesArray = new string[linesCollection.Count];
  78. linesCollection.CopyTo(linesArray, 0);
  79. this.textBoxContents.Lines = linesArray;
  80. }
  81. StringCollection ReadFileIntoStringCollection()
  82. {
  83. const int MaxBytes = 65536;
  84. StreamReader sr = new StreamReader(chosenFile);
  85. StringCollection result = new StringCollection();
  86. int nBytesRead = 0;
  87. string nextLine;
  88. while ( (nextLine = sr.ReadLine()) != null)
  89. {
  90. nBytesRead += nextLine.Length;
  91. if (nBytesRead > MaxBytes)
  92. break;
  93. result.Add(nextLine);
  94. }
  95. sr.Close();
  96. return result;
  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.menuFileSave = new System.Windows.Forms.MenuItem();
  110. this.SuspendLayout();
  111. // 
  112. // textBoxContents
  113. // 
  114. this.textBoxContents.Dock = System.Windows.Forms.DockStyle.Fill;
  115. this.textBoxContents.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  116. this.textBoxContents.Multiline = true;
  117. this.textBoxContents.Name = "textBoxContents";
  118. this.textBoxContents.ScrollBars = System.Windows.Forms.ScrollBars.Both;
  119. this.textBoxContents.Size = new System.Drawing.Size(292, 268);
  120. this.textBoxContents.TabIndex = 0;
  121. this.textBoxContents.Text = "";
  122. // 
  123. // mainMenu1
  124. // 
  125. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  126.   this.menuItem1});
  127. // 
  128. // menuItem1
  129. // 
  130. this.menuItem1.Index = 0;
  131. this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  132.   this.menuFileOpen,
  133.   this.menuFileSave});
  134. this.menuItem1.Text = "&File";
  135. // 
  136. // menuFileOpen
  137. // 
  138. this.menuFileOpen.Index = 0;
  139. this.menuFileOpen.Text = "&Open";
  140. // 
  141. // menuFileSave
  142. // 
  143. this.menuFileSave.Index = 1;
  144. this.menuFileSave.Text = "&Save";
  145. this.menuFileSave.Click += new System.EventHandler(this.OnFileSave);
  146. // 
  147. // Form1
  148. // 
  149. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  150. this.ClientSize = new System.Drawing.Size(292, 268);
  151. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  152.   this.textBoxContents});
  153. this.Menu = this.mainMenu1;
  154. this.Name = "Form1";
  155. this.Text = "ReadWriteText";
  156. this.ResumeLayout(false);
  157. }
  158. #endregion
  159. /// <summary>
  160. /// The main entry point for the application.
  161. /// </summary>
  162. [STAThread]
  163. static void Main() 
  164. {
  165. Application.Run(new Form1());
  166. }
  167. }
  168. }