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

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. namespace Wrox.ProCSharp.FilesAndRegistry
  9. {
  10. /// <summary>
  11. /// Summary description for Form1.
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private string currentFolderPath;
  16. private System.Windows.Forms.Label label8;
  17. private System.Windows.Forms.Button buttonDisplay;
  18. private System.Windows.Forms.Button buttonUp;
  19. private System.Windows.Forms.TextBox txtBoxLastWriteTime;
  20. private System.Windows.Forms.Label label2;
  21. private System.Windows.Forms.TextBox txtBoxInput;
  22. private System.Windows.Forms.Label label1;
  23. private System.Windows.Forms.TextBox txtBoxCreationTime;
  24. private System.Windows.Forms.ListBox listBoxFiles;
  25. private System.Windows.Forms.ListBox listBoxFolders;
  26. private System.Windows.Forms.GroupBox groupBox1;
  27. private System.Windows.Forms.Label label6;
  28. private System.Windows.Forms.TextBox txtBoxLastAccessTime;
  29. private System.Windows.Forms.Label label5;
  30. private System.Windows.Forms.Label label3;
  31. private System.Windows.Forms.TextBox txtBoxFileSize;
  32. private System.Windows.Forms.Label label7;
  33. private System.Windows.Forms.TextBox txtBoxFileName;
  34. private System.Windows.Forms.Label label4;
  35. private System.Windows.Forms.TextBox txtBoxFolder;
  36. private System.Windows.Forms.GroupBox groupBox2;
  37. private System.Windows.Forms.GroupBox groupBox3;
  38. private System.Windows.Forms.Label label9;
  39. private System.Windows.Forms.TextBox textBox1;
  40. private System.Windows.Forms.Label label10;
  41. private System.Windows.Forms.Label label11;
  42. private System.Windows.Forms.TextBox textBox2;
  43. private System.Windows.Forms.Label label12;
  44. private System.Windows.Forms.TextBox textBox3;
  45. private System.Windows.Forms.Label label13;
  46. private System.Windows.Forms.TextBox textBox4;
  47. private System.Windows.Forms.GroupBox groupBox4;
  48. private System.Windows.Forms.Button button1;
  49. private System.Windows.Forms.TextBox textBox5;
  50. /// <summary>
  51. /// Required designer variable.
  52. /// </summary>
  53. private System.ComponentModel.Container components = null;
  54. public Form1()
  55. {
  56. //
  57. // Required for Windows Form Designer support
  58. //
  59. InitializeComponent();
  60. //
  61. // TODO: Add any constructor code after InitializeComponent call
  62. //
  63. }
  64. /// <summary>
  65. /// Clean up any resources being used.
  66. /// </summary>
  67. protected override void Dispose( bool disposing )
  68. {
  69. if( disposing )
  70. {
  71. if (components != null) 
  72. {
  73. components.Dispose();
  74. }
  75. }
  76. base.Dispose( disposing );
  77. }
  78. protected void ClearAllFields()
  79. {
  80. listBoxFolders.Items.Clear();
  81. listBoxFiles.Items.Clear();
  82. txtBoxFolder.Text = "";
  83. txtBoxFileName.Text = "";
  84. txtBoxCreationTime.Text = "";
  85. txtBoxLastAccessTime.Text = "";
  86. txtBoxLastWriteTime.Text = "";
  87. txtBoxFileSize.Text = "";
  88. }
  89. protected void DisplayFileInfo(string fileFullName)
  90. {
  91. FileInfo theFile = new FileInfo(fileFullName);
  92. if (!theFile.Exists)
  93. throw new FileNotFoundException("File not found: " + fileFullName);
  94. txtBoxFileName.Text = theFile.Name;
  95. txtBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString();
  96. txtBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString();
  97. txtBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString();
  98. txtBoxFileSize.Text = theFile.Length.ToString() + " bytes";
  99. }
  100. protected void DisplayFolderList(string folderFullName)
  101. {
  102. DirectoryInfo theFolder = new DirectoryInfo(folderFullName);
  103. if (!theFolder.Exists)
  104. throw new DirectoryNotFoundException("Folder not found: " 
  105. + folderFullName);
  106. ClearAllFields();
  107. txtBoxFolder.Text = theFolder.FullName;
  108. currentFolderPath = theFolder.FullName; 
  109. // list all subfolders in folder
  110. foreach(DirectoryInfo nextFolder in theFolder.GetDirectories())
  111. listBoxFolders.Items.Add(nextFolder.Name);
  112. // list all files in folder
  113. foreach(FileInfo nextFile in theFolder.GetFiles())
  114. listBoxFiles.Items.Add(nextFile.Name);
  115. }
  116. protected void OnDisplayButtonClick(object sender, EventArgs e)
  117. {
  118. try
  119. {
  120. string folderPath = txtBoxInput.Text;
  121. DirectoryInfo theFolder = new DirectoryInfo(folderPath);
  122. if (theFolder.Exists)
  123. {
  124. DisplayFolderList(theFolder.FullName);
  125. return;
  126. }
  127. FileInfo theFile = new FileInfo(folderPath);
  128. if (theFile.Exists)
  129. {
  130. DisplayFolderList(theFile.Directory.FullName);
  131. int index = listBoxFiles.Items.IndexOf(theFile.Name);
  132. listBoxFiles.SetSelected(index, true);
  133. return;
  134. }
  135. throw new FileNotFoundException("There is no file or folder with "
  136. + "this name: " + txtBoxInput.Text);
  137. }
  138. catch(Exception ex)
  139. {
  140. MessageBox.Show(ex.Message);
  141. }
  142. }
  143. protected void OnListBoxFilesSelected(object sender, EventArgs e)
  144. {
  145. try
  146. {
  147. string selectedString = listBoxFiles.SelectedItem.ToString();
  148. string fullFileName = Path.Combine(currentFolderPath, selectedString);
  149. DisplayFileInfo(fullFileName);
  150. }
  151. catch(Exception ex)
  152. {
  153. MessageBox.Show(ex.Message);
  154. }
  155. }
  156. protected void OnListBoxFoldersSelected(object sender, EventArgs e)
  157. {
  158. try
  159. {
  160. string selectedString = listBoxFolders.SelectedItem.ToString();
  161. string fullPathName = Path.Combine(currentFolderPath, selectedString);
  162. DisplayFolderList(fullPathName);
  163. }
  164. catch(Exception ex)
  165. {
  166. MessageBox.Show(ex.Message);
  167. }
  168. }
  169. protected void OnUpButtonClick(object sender, EventArgs e)
  170. {
  171. try
  172. {
  173. string folderPath = new FileInfo(currentFolderPath).DirectoryName;
  174. DisplayFolderList(folderPath);
  175. }
  176. catch(Exception ex)
  177. {
  178. MessageBox.Show(ex.Message);
  179. }
  180. }
  181. #region Windows Form Designer generated code
  182. /// <summary>
  183. /// Required method for Designer support - do not modify
  184. /// the contents of this method with the code editor.
  185. /// </summary>
  186. private void InitializeComponent()
  187. {
  188. this.label8 = new System.Windows.Forms.Label();
  189. this.buttonDisplay = new System.Windows.Forms.Button();
  190. this.buttonUp = new System.Windows.Forms.Button();
  191. this.txtBoxLastWriteTime = new System.Windows.Forms.TextBox();
  192. this.label2 = new System.Windows.Forms.Label();
  193. this.txtBoxInput = new System.Windows.Forms.TextBox();
  194. this.label1 = new System.Windows.Forms.Label();
  195. this.txtBoxCreationTime = new System.Windows.Forms.TextBox();
  196. this.listBoxFiles = new System.Windows.Forms.ListBox();
  197. this.listBoxFolders = new System.Windows.Forms.ListBox();
  198. this.groupBox1 = new System.Windows.Forms.GroupBox();
  199. this.label6 = new System.Windows.Forms.Label();
  200. this.txtBoxLastAccessTime = new System.Windows.Forms.TextBox();
  201. this.label5 = new System.Windows.Forms.Label();
  202. this.label3 = new System.Windows.Forms.Label();
  203. this.txtBoxFileSize = new System.Windows.Forms.TextBox();
  204. this.label7 = new System.Windows.Forms.Label();
  205. this.txtBoxFileName = new System.Windows.Forms.TextBox();
  206. this.label4 = new System.Windows.Forms.Label();
  207. this.txtBoxFolder = new System.Windows.Forms.TextBox();
  208. this.groupBox2 = new System.Windows.Forms.GroupBox();
  209. this.groupBox3 = new System.Windows.Forms.GroupBox();
  210. this.label9 = new System.Windows.Forms.Label();
  211. this.textBox1 = new System.Windows.Forms.TextBox();
  212. this.label10 = new System.Windows.Forms.Label();
  213. this.label11 = new System.Windows.Forms.Label();
  214. this.textBox2 = new System.Windows.Forms.TextBox();
  215. this.label12 = new System.Windows.Forms.Label();
  216. this.textBox3 = new System.Windows.Forms.TextBox();
  217. this.label13 = new System.Windows.Forms.Label();
  218. this.textBox4 = new System.Windows.Forms.TextBox();
  219. this.groupBox4 = new System.Windows.Forms.GroupBox();
  220. this.button1 = new System.Windows.Forms.Button();
  221. this.textBox5 = new System.Windows.Forms.TextBox();
  222. this.groupBox1.SuspendLayout();
  223. this.groupBox3.SuspendLayout();
  224. this.SuspendLayout();
  225. // 
  226. // label8
  227. // 
  228. this.label8.Location = new System.Drawing.Point(256, 104);
  229. this.label8.Name = "label8";
  230. this.label8.Size = new System.Drawing.Size(100, 16);
  231. this.label8.TabIndex = 14;
  232. this.label8.Text = "Folders";
  233. // 
  234. // buttonDisplay
  235. // 
  236. this.buttonDisplay.Location = new System.Drawing.Point(432, 24);
  237. this.buttonDisplay.Name = "buttonDisplay";
  238. this.buttonDisplay.TabIndex = 26;
  239. this.buttonDisplay.Text = "Display";
  240. this.buttonDisplay.Click += new System.EventHandler(this.OnDisplayButtonClick);
  241. // 
  242. // buttonUp
  243. // 
  244. this.buttonUp.Location = new System.Drawing.Point(408, 72);
  245. this.buttonUp.Name = "buttonUp";
  246. this.buttonUp.Size = new System.Drawing.Size(72, 24);
  247. this.buttonUp.TabIndex = 24;
  248. this.buttonUp.Text = "Up";
  249. this.buttonUp.Click += new System.EventHandler(this.OnUpButtonClick);
  250. // 
  251. // txtBoxLastWriteTime
  252. // 
  253. this.txtBoxLastWriteTime.Enabled = false;
  254. this.txtBoxLastWriteTime.Location = new System.Drawing.Point(32, 448);
  255. this.txtBoxLastWriteTime.Name = "txtBoxLastWriteTime";
  256. this.txtBoxLastWriteTime.Size = new System.Drawing.Size(184, 20);
  257. this.txtBoxLastWriteTime.TabIndex = 16;
  258. this.txtBoxLastWriteTime.Text = "";
  259. // 
  260. // label2
  261. // 
  262. this.label2.Location = new System.Drawing.Point(16, 104);
  263. this.label2.Name = "label2";
  264. this.label2.Size = new System.Drawing.Size(136, 16);
  265. this.label2.TabIndex = 22;
  266. this.label2.Text = "Files";
  267. // 
  268. // txtBoxInput
  269. // 
  270. this.txtBoxInput.Location = new System.Drawing.Point(16, 24);
  271. this.txtBoxInput.Name = "txtBoxInput";
  272. this.txtBoxInput.Size = new System.Drawing.Size(408, 20);
  273. this.txtBoxInput.TabIndex = 19;
  274. this.txtBoxInput.Text = "";
  275. // 
  276. // label1
  277. // 
  278. this.label1.Location = new System.Drawing.Point(16, 8);
  279. this.label1.Name = "label1";
  280. this.label1.Size = new System.Drawing.Size(280, 16);
  281. this.label1.TabIndex = 20;
  282. this.label1.Text = "Enter name of folder to be examined and click Display";
  283. // 
  284. // txtBoxCreationTime
  285. // 
  286. this.txtBoxCreationTime.Enabled = false;
  287. this.txtBoxCreationTime.Location = new System.Drawing.Point(248, 400);
  288. this.txtBoxCreationTime.Name = "txtBoxCreationTime";
  289. this.txtBoxCreationTime.Size = new System.Drawing.Size(184, 20);
  290. this.txtBoxCreationTime.TabIndex = 15;
  291. this.txtBoxCreationTime.Text = "";
  292. // 
  293. // listBoxFiles
  294. // 
  295. this.listBoxFiles.Location = new System.Drawing.Point(16, 120);
  296. this.listBoxFiles.Name = "listBoxFiles";
  297. this.listBoxFiles.Size = new System.Drawing.Size(240, 199);
  298. this.listBoxFiles.TabIndex = 13;
  299. this.listBoxFiles.SelectedIndexChanged += new System.EventHandler(this.OnListBoxFilesSelected);
  300. // 
  301. // listBoxFolders
  302. // 
  303. this.listBoxFolders.Location = new System.Drawing.Point(264, 120);
  304. this.listBoxFolders.Name = "listBoxFolders";
  305. this.listBoxFolders.Size = new System.Drawing.Size(240, 199);
  306. this.listBoxFolders.TabIndex = 17;
  307. this.listBoxFolders.SelectedIndexChanged += new System.EventHandler(this.OnListBoxFoldersSelected);
  308. // 
  309. // groupBox1
  310. // 
  311. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  312. this.label6,
  313. this.txtBoxLastAccessTime,
  314. this.label5,
  315. this.label3,
  316. this.txtBoxFileSize,
  317. this.label7,
  318. this.txtBoxFileName,
  319. this.label4,
  320. this.txtBoxFolder,
  321. this.groupBox2});
  322. this.groupBox1.Location = new System.Drawing.Point(8, 48);
  323. this.groupBox1.Name = "groupBox1";
  324. this.groupBox1.Size = new System.Drawing.Size(496, 432);
  325. this.groupBox1.TabIndex = 27;
  326. this.groupBox1.TabStop = false;
  327. this.groupBox1.Text = "Contents of folder";
  328. // 
  329. // label6
  330. // 
  331. this.label6.Location = new System.Drawing.Point(24, 384);
  332. this.label6.Name = "label6";
  333. this.label6.Size = new System.Drawing.Size(120, 16);
  334. this.label6.TabIndex = 8;
  335. this.label6.Text = "Last modification time";
  336. // 
  337. // txtBoxLastAccessTime
  338. // 
  339. this.txtBoxLastAccessTime.Enabled = false;
  340. this.txtBoxLastAccessTime.Location = new System.Drawing.Point(240, 400);
  341. this.txtBoxLastAccessTime.Name = "txtBoxLastAccessTime";
  342. this.txtBoxLastAccessTime.Size = new System.Drawing.Size(184, 20);
  343. this.txtBoxLastAccessTime.TabIndex = 1;
  344. this.txtBoxLastAccessTime.Text = "";
  345. // 
  346. // label5
  347. // 
  348. this.label5.Location = new System.Drawing.Point(240, 376);
  349. this.label5.Name = "label5";
  350. this.label5.Size = new System.Drawing.Size(100, 16);
  351. this.label5.TabIndex = 7;
  352. this.label5.Text = "Last access time";
  353. // 
  354. // label3
  355. // 
  356. this.label3.Location = new System.Drawing.Point(240, 328);
  357. this.label3.Name = "label3";
  358. this.label3.Size = new System.Drawing.Size(100, 16);
  359. this.label3.TabIndex = 5;
  360. this.label3.Text = "Creation time";
  361. // 
  362. // txtBoxFileSize
  363. // 
  364. this.txtBoxFileSize.Enabled = false;
  365. this.txtBoxFileSize.Location = new System.Drawing.Point(24, 352);
  366. this.txtBoxFileSize.Name = "txtBoxFileSize";
  367. this.txtBoxFileSize.Size = new System.Drawing.Size(184, 20);
  368. this.txtBoxFileSize.TabIndex = 1;
  369. this.txtBoxFileSize.Text = "";
  370. // 
  371. // label7
  372. // 
  373. this.label7.Location = new System.Drawing.Point(24, 328);
  374. this.label7.Name = "label7";
  375. this.label7.Size = new System.Drawing.Size(100, 16);
  376. this.label7.TabIndex = 10;
  377. this.label7.Text = "File Size";
  378. // 
  379. // txtBoxFileName
  380. // 
  381. this.txtBoxFileName.Enabled = false;
  382. this.txtBoxFileName.Location = new System.Drawing.Point(80, 304);
  383. this.txtBoxFileName.Name = "txtBoxFileName";
  384. this.txtBoxFileName.Size = new System.Drawing.Size(400, 20);
  385. this.txtBoxFileName.TabIndex = 1;
  386. this.txtBoxFileName.Text = "";
  387. // 
  388. // label4
  389. // 
  390. this.label4.Location = new System.Drawing.Point(16, 304);
  391. this.label4.Name = "label4";
  392. this.label4.Size = new System.Drawing.Size(64, 16);
  393. this.label4.TabIndex = 6;
  394. this.label4.Text = "File name";
  395. // 
  396. // txtBoxFolder
  397. // 
  398. this.txtBoxFolder.Enabled = false;
  399. this.txtBoxFolder.Location = new System.Drawing.Point(8, 24);
  400. this.txtBoxFolder.Name = "txtBoxFolder";
  401. this.txtBoxFolder.Size = new System.Drawing.Size(384, 20);
  402. this.txtBoxFolder.TabIndex = 2;
  403. this.txtBoxFolder.Text = "";
  404. // 
  405. // groupBox2
  406. // 
  407. this.groupBox2.Location = new System.Drawing.Point(8, 280);
  408. this.groupBox2.Name = "groupBox2";
  409. this.groupBox2.Size = new System.Drawing.Size(480, 144);
  410. this.groupBox2.TabIndex = 11;
  411. this.groupBox2.TabStop = false;
  412. this.groupBox2.Text = "Details of Selected File";
  413. // 
  414. // groupBox3
  415. // 
  416. this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
  417. this.label9,
  418. this.textBox1,
  419. this.label10,
  420. this.label11,
  421. this.textBox2,
  422. this.label12,
  423. this.textBox3,
  424. this.label13,
  425. this.textBox4,
  426. this.groupBox4});
  427. this.groupBox3.Location = new System.Drawing.Point(8, 48);
  428. this.groupBox3.Name = "groupBox3";
  429. this.groupBox3.Size = new System.Drawing.Size(496, 432);
  430. this.groupBox3.TabIndex = 28;
  431. this.groupBox3.TabStop = false;
  432. this.groupBox3.Text = "Contents of folder";
  433. // 
  434. // label9
  435. // 
  436. this.label9.Location = new System.Drawing.Point(24, 384);
  437. this.label9.Name = "label9";
  438. this.label9.Size = new System.Drawing.Size(120, 16);
  439. this.label9.TabIndex = 8;
  440. this.label9.Text = "Last modification time";
  441. // 
  442. // textBox1
  443. // 
  444. this.textBox1.Enabled = false;
  445. this.textBox1.Location = new System.Drawing.Point(240, 400);
  446. this.textBox1.Name = "textBox1";
  447. this.textBox1.Size = new System.Drawing.Size(184, 20);
  448. this.textBox1.TabIndex = 1;
  449. this.textBox1.Text = "";
  450. // 
  451. // label10
  452. // 
  453. this.label10.Location = new System.Drawing.Point(240, 376);
  454. this.label10.Name = "label10";
  455. this.label10.Size = new System.Drawing.Size(100, 16);
  456. this.label10.TabIndex = 7;
  457. this.label10.Text = "Last access time";
  458. // 
  459. // label11
  460. // 
  461. this.label11.Location = new System.Drawing.Point(240, 328);
  462. this.label11.Name = "label11";
  463. this.label11.Size = new System.Drawing.Size(100, 16);
  464. this.label11.TabIndex = 5;
  465. this.label11.Text = "Creation time";
  466. // 
  467. // textBox2
  468. // 
  469. this.textBox2.Enabled = false;
  470. this.textBox2.Location = new System.Drawing.Point(24, 352);
  471. this.textBox2.Name = "textBox2";
  472. this.textBox2.Size = new System.Drawing.Size(184, 20);
  473. this.textBox2.TabIndex = 1;
  474. this.textBox2.Text = "";
  475. // 
  476. // label12
  477. // 
  478. this.label12.Location = new System.Drawing.Point(24, 328);
  479. this.label12.Name = "label12";
  480. this.label12.Size = new System.Drawing.Size(100, 16);
  481. this.label12.TabIndex = 10;
  482. this.label12.Text = "File Size";
  483. // 
  484. // textBox3
  485. // 
  486. this.textBox3.Enabled = false;
  487. this.textBox3.Location = new System.Drawing.Point(80, 304);
  488. this.textBox3.Name = "textBox3";
  489. this.textBox3.Size = new System.Drawing.Size(400, 20);
  490. this.textBox3.TabIndex = 1;
  491. this.textBox3.Text = "";
  492. // 
  493. // label13
  494. // 
  495. this.label13.Location = new System.Drawing.Point(16, 304);
  496. this.label13.Name = "label13";
  497. this.label13.Size = new System.Drawing.Size(64, 16);
  498. this.label13.TabIndex = 6;
  499. this.label13.Text = "File name";
  500. // 
  501. // textBox4
  502. // 
  503. this.textBox4.Enabled = false;
  504. this.textBox4.Location = new System.Drawing.Point(8, 24);
  505. this.textBox4.Name = "textBox4";
  506. this.textBox4.Size = new System.Drawing.Size(384, 20);
  507. this.textBox4.TabIndex = 2;
  508. this.textBox4.Text = "";
  509. // 
  510. // groupBox4
  511. // 
  512. this.groupBox4.Location = new System.Drawing.Point(8, 280);
  513. this.groupBox4.Name = "groupBox4";
  514. this.groupBox4.Size = new System.Drawing.Size(480, 144);
  515. this.groupBox4.TabIndex = 11;
  516. this.groupBox4.TabStop = false;
  517. this.groupBox4.Text = "If file selected ...";
  518. // 
  519. // button1
  520. // 
  521. this.button1.Location = new System.Drawing.Point(400, 72);
  522. this.button1.Name = "button1";
  523. this.button1.Size = new System.Drawing.Size(72, 24);
  524. this.button1.TabIndex = 23;
  525. this.button1.Text = "Up";
  526. // 
  527. // textBox5
  528. // 
  529. this.textBox5.Location = new System.Drawing.Point(16, 24);
  530. this.textBox5.Name = "textBox5";
  531. this.textBox5.Size = new System.Drawing.Size(408, 20);
  532. this.textBox5.TabIndex = 18;
  533. this.textBox5.Text = "";
  534. // 
  535. // Form1
  536. // 
  537. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  538. this.ClientSize = new System.Drawing.Size(520, 496);
  539. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  540.   this.label8,
  541.   this.buttonDisplay,
  542.   this.buttonUp,
  543.   this.txtBoxLastWriteTime,
  544.   this.label2,
  545.   this.txtBoxInput,
  546.   this.label1,
  547.   this.txtBoxCreationTime,
  548.   this.listBoxFiles,
  549.   this.listBoxFolders,
  550.   this.groupBox1,
  551.   this.groupBox3,
  552.   this.button1,
  553.   this.textBox5});
  554. this.Name = "Form1";
  555. this.Text = "FileProperties Sample";
  556. this.groupBox1.ResumeLayout(false);
  557. this.groupBox3.ResumeLayout(false);
  558. this.ResumeLayout(false);
  559. }
  560. #endregion
  561. /// <summary>
  562. /// The main entry point for the application.
  563. /// </summary>
  564. [STAThread]
  565. static void Main() 
  566. {
  567. Application.Run(new Form1());
  568. }
  569. }
  570. }