MainForm.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:17k
源码类别:

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. //Add by myself
  8. using System.IO;
  9. namespace FileManager
  10. {
  11. /// <summary>
  12. /// MainForm 的摘要说明。
  13. /// </summary>
  14. public class MainForm : System.Windows.Forms.Form
  15. {
  16. private System.Windows.Forms.MainMenu mainMenu1;
  17. private System.Windows.Forms.Label labelCurrentPath;
  18. private System.Windows.Forms.TextBox txtCurPath;
  19. private System.Windows.Forms.Button btnUp;
  20. private System.Windows.Forms.ListView FilesView;
  21. private System.Windows.Forms.ImageList imageLarge;
  22. private System.Windows.Forms.ImageList imageSmall;
  23. private System.Windows.Forms.ContextMenu contextMenu;
  24. private System.ComponentModel.IContainer components;
  25. private System.Windows.Forms.MenuItem FileMenu;
  26. private System.Windows.Forms.MenuItem NewMenu;
  27. private System.Windows.Forms.MenuItem NewFolderMenu;
  28. private System.Windows.Forms.MenuItem NewFileMenu;
  29. private System.Windows.Forms.MenuItem ExitMenu;
  30. private System.Windows.Forms.MenuItem EditMenu;
  31. private System.Windows.Forms.MenuItem DeleteMenu;
  32. private System.Windows.Forms.MenuItem ContextmenuNew;
  33. private System.Windows.Forms.MenuItem ContextmenuNewFolder;
  34. private System.Windows.Forms.MenuItem ContextmenuNewFile;
  35. private System.Windows.Forms.MenuItem ContextmenuDelete;
  36. //CurPath
  37. System.Collections.Specialized.StringCollection CurPath= new System.Collections.Specialized.StringCollection();
  38. public MainForm()
  39. {
  40. //
  41. // Windows 窗体设计器支持所必需的
  42. //
  43. InitializeComponent();
  44. //
  45. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  46. //
  47. try 
  48. {
  49. //Initial path is "My Computer"
  50. this.txtCurPath.Text="My Computer";
  51. //Clear FilesView
  52. this.FilesView.Clear();
  53. //set FilesView
  54. this.FilesView.View= View.Details;
  55. this.FilesView.Columns.Add("本地磁盘",FilesView.Width/3,HorizontalAlignment.Left);
  56. //using the method "GetLogicalDrives()"
  57. string[] Drv= Directory.GetLogicalDrives();
  58. int DrvCnt= Drv.Length;
  59. //Display the disk
  60. for(int i=0;i<DrvCnt;i++) 
  61. {
  62. ListViewItem lvi= new ListViewItem();
  63. lvi.Text="驱动器"+Drv[i];
  64. lvi.ImageIndex=3;
  65. lvi.Tag=Drv[i];
  66. this.FilesView.Items.Add(lvi);
  67. }
  68. }
  69. catch(Exception ex) 
  70. {
  71. MessageBox.Show(ex.Message);
  72. }
  73. }
  74. /// <summary>
  75. /// 清理所有正在使用的资源。
  76. /// </summary>
  77. protected override void Dispose( bool disposing )
  78. {
  79. if( disposing )
  80. {
  81. if (components != null) 
  82. {
  83. components.Dispose();
  84. }
  85. }
  86. base.Dispose( disposing );
  87. }
  88. #region Windows 窗体设计器生成的代码
  89. /// <summary>
  90. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  91. /// 此方法的内容。
  92. /// </summary>
  93. private void InitializeComponent()
  94. {
  95. this.components = new System.ComponentModel.Container();
  96. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
  97. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  98. this.FileMenu = new System.Windows.Forms.MenuItem();
  99. this.NewMenu = new System.Windows.Forms.MenuItem();
  100. this.NewFolderMenu = new System.Windows.Forms.MenuItem();
  101. this.NewFileMenu = new System.Windows.Forms.MenuItem();
  102. this.ExitMenu = new System.Windows.Forms.MenuItem();
  103. this.EditMenu = new System.Windows.Forms.MenuItem();
  104. this.DeleteMenu = new System.Windows.Forms.MenuItem();
  105. this.labelCurrentPath = new System.Windows.Forms.Label();
  106. this.txtCurPath = new System.Windows.Forms.TextBox();
  107. this.btnUp = new System.Windows.Forms.Button();
  108. this.FilesView = new System.Windows.Forms.ListView();
  109. this.contextMenu = new System.Windows.Forms.ContextMenu();
  110. this.ContextmenuNew = new System.Windows.Forms.MenuItem();
  111. this.ContextmenuNewFolder = new System.Windows.Forms.MenuItem();
  112. this.ContextmenuNewFile = new System.Windows.Forms.MenuItem();
  113. this.ContextmenuDelete = new System.Windows.Forms.MenuItem();
  114. this.imageLarge = new System.Windows.Forms.ImageList(this.components);
  115. this.imageSmall = new System.Windows.Forms.ImageList(this.components);
  116. this.SuspendLayout();
  117. // 
  118. // mainMenu1
  119. // 
  120. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  121.   this.FileMenu,
  122.   this.EditMenu});
  123. // 
  124. // FileMenu
  125. // 
  126. this.FileMenu.Index = 0;
  127. this.FileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  128.  this.NewMenu,
  129.  this.ExitMenu});
  130. this.FileMenu.Text = "文件";
  131. // 
  132. // NewMenu
  133. // 
  134. this.NewMenu.Index = 0;
  135. this.NewMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  136. this.NewFolderMenu,
  137. this.NewFileMenu});
  138. this.NewMenu.Text = "新建";
  139. this.NewMenu.Click += new System.EventHandler(this.NewMenu_Click);
  140. // 
  141. // NewFolderMenu
  142. // 
  143. this.NewFolderMenu.Index = 0;
  144. this.NewFolderMenu.Text = "文件夹";
  145. this.NewFolderMenu.Click += new System.EventHandler(this.NewFolderMenu_Click);
  146. // 
  147. // NewFileMenu
  148. // 
  149. this.NewFileMenu.Index = 1;
  150. this.NewFileMenu.Text = "文件";
  151. this.NewFileMenu.Click += new System.EventHandler(this.NewFileMenu_Click);
  152. // 
  153. // ExitMenu
  154. // 
  155. this.ExitMenu.Index = 1;
  156. this.ExitMenu.Text = "退出";
  157. this.ExitMenu.Click += new System.EventHandler(this.ExitMenu_Click);
  158. // 
  159. // EditMenu
  160. // 
  161. this.EditMenu.Index = 1;
  162. this.EditMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  163.  this.DeleteMenu});
  164. this.EditMenu.Text = "编辑";
  165. // 
  166. // DeleteMenu
  167. // 
  168. this.DeleteMenu.Index = 0;
  169. this.DeleteMenu.Text = "删除";
  170. this.DeleteMenu.Click += new System.EventHandler(this.DeleteMenu_Click);
  171. // 
  172. // labelCurrentPath
  173. // 
  174. this.labelCurrentPath.Location = new System.Drawing.Point(56, 48);
  175. this.labelCurrentPath.Name = "labelCurrentPath";
  176. this.labelCurrentPath.Size = new System.Drawing.Size(72, 23);
  177. this.labelCurrentPath.TabIndex = 0;
  178. this.labelCurrentPath.Text = "路径:";
  179. // 
  180. // txtCurPath
  181. // 
  182. this.txtCurPath.Location = new System.Drawing.Point(160, 48);
  183. this.txtCurPath.Name = "txtCurPath";
  184. this.txtCurPath.ReadOnly = true;
  185. this.txtCurPath.Size = new System.Drawing.Size(296, 21);
  186. this.txtCurPath.TabIndex = 1;
  187. this.txtCurPath.Text = "";
  188. this.txtCurPath.TextChanged += new System.EventHandler(this.txtCurPath_TextChanged);
  189. // 
  190. // btnUp
  191. // 
  192. this.btnUp.Location = new System.Drawing.Point(472, 48);
  193. this.btnUp.Name = "btnUp";
  194. this.btnUp.Size = new System.Drawing.Size(104, 23);
  195. this.btnUp.TabIndex = 2;
  196. this.btnUp.Text = "返回上一层";
  197. this.btnUp.Click += new System.EventHandler(this.btnUp_Click);
  198. // 
  199. // FilesView
  200. // 
  201. this.FilesView.ContextMenu = this.contextMenu;
  202. this.FilesView.LargeImageList = this.imageLarge;
  203. this.FilesView.Location = new System.Drawing.Point(40, 96);
  204. this.FilesView.Name = "FilesView";
  205. this.FilesView.Size = new System.Drawing.Size(624, 328);
  206. this.FilesView.SmallImageList = this.imageSmall;
  207. this.FilesView.TabIndex = 3;
  208. this.FilesView.View = System.Windows.Forms.View.Details;
  209. this.FilesView.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FilesView_KeyDown);
  210. this.FilesView.DoubleClick += new System.EventHandler(this.FilesView_DoubleClick);
  211. this.FilesView.SelectedIndexChanged += new System.EventHandler(this.FilesView_SelectedIndexChanged);
  212. // 
  213. // contextMenu
  214. // 
  215. this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  216. this.ContextmenuNew,
  217. this.ContextmenuDelete});
  218. // 
  219. // ContextmenuNew
  220. // 
  221. this.ContextmenuNew.Index = 0;
  222. this.ContextmenuNew.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  223.    this.ContextmenuNewFolder,
  224.    this.ContextmenuNewFile});
  225. this.ContextmenuNew.Text = "新建";
  226. // 
  227. // ContextmenuNewFolder
  228. // 
  229. this.ContextmenuNewFolder.Index = 0;
  230. this.ContextmenuNewFolder.Text = "文件夹";
  231. this.ContextmenuNewFolder.Click += new System.EventHandler(this.ContextmenuNewFolder_Click);
  232. // 
  233. // ContextmenuNewFile
  234. // 
  235. this.ContextmenuNewFile.Index = 1;
  236. this.ContextmenuNewFile.Text = "文件";
  237. this.ContextmenuNewFile.Click += new System.EventHandler(this.ContextmenuNewFile_Click);
  238. // 
  239. // ContextmenuDelete
  240. // 
  241. this.ContextmenuDelete.Index = 1;
  242. this.ContextmenuDelete.Text = "删除";
  243. this.ContextmenuDelete.Click += new System.EventHandler(this.ContextmenuDelete_Click);
  244. // 
  245. // imageLarge
  246. // 
  247. this.imageLarge.ImageSize = new System.Drawing.Size(16, 16);
  248. this.imageLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageLarge.ImageStream")));
  249. this.imageLarge.TransparentColor = System.Drawing.Color.Transparent;
  250. // 
  251. // imageSmall
  252. // 
  253. this.imageSmall.ImageSize = new System.Drawing.Size(16, 16);
  254. this.imageSmall.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageSmall.ImageStream")));
  255. this.imageSmall.TransparentColor = System.Drawing.Color.Transparent;
  256. // 
  257. // MainForm
  258. // 
  259. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  260. this.ClientSize = new System.Drawing.Size(704, 433);
  261. this.Controls.Add(this.FilesView);
  262. this.Controls.Add(this.btnUp);
  263. this.Controls.Add(this.txtCurPath);
  264. this.Controls.Add(this.labelCurrentPath);
  265. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
  266. this.MaximizeBox = false;
  267. this.Menu = this.mainMenu1;
  268. this.Name = "MainForm";
  269. this.Text = "FileManager";
  270. this.ResumeLayout(false);
  271. }
  272. #endregion
  273. /// <summary>
  274. /// 应用程序的主入口点。
  275. /// </summary>
  276. [STAThread]
  277. static void Main() 
  278. {
  279. Application.Run(new MainForm());
  280. }
  281. //Add method
  282. //FillFilesView()
  283. public void FillFilesView(string FullName) 
  284. {
  285. FilesView.Clear();
  286. FilesView.View= View.Details;
  287. //Add Column
  288. FilesView.Columns.Add("名称",FilesView.Width/3,HorizontalAlignment.Left);
  289. FilesView.Columns.Add("类型",FilesView.Width/6,HorizontalAlignment.Center);
  290. FilesView.Columns.Add("大小",FilesView.Width/6,HorizontalAlignment.Right);
  291. FilesView.Columns.Add("最后访问时间",FilesView.Width/3,HorizontalAlignment.Left);
  292. DirectoryInfo CurDir= new DirectoryInfo(FullName);
  293. DirectoryInfo[] dirs=CurDir.GetDirectories();
  294. FileInfo[] files=CurDir.GetFiles();
  295. //Display Folders in current path
  296. foreach(DirectoryInfo dir in dirs) 
  297. {
  298. ListViewItem lvi= new ListViewItem();
  299. lvi.Text=dir.Name;
  300. lvi.ImageIndex=0;
  301. lvi.Tag=dir.FullName;
  302. lvi.SubItems.Add("文件夹");
  303. lvi.SubItems.Add("");
  304. lvi.SubItems.Add(dir.LastAccessTime.ToString());
  305. FilesView.Items.Add(lvi);
  306. }
  307. //Display Files in current path
  308. foreach(FileInfo file in files) 
  309. {
  310. ListViewItem lvi= new ListViewItem();
  311. lvi.Text=file.Name;
  312. //txt file
  313. if(file.Extension==".txt") 
  314. {
  315. lvi.ImageIndex=1;
  316. }
  317. else 
  318. {
  319. lvi.ImageIndex=2;
  320. }
  321. lvi.Tag=file.FullName;
  322. lvi.SubItems.Add("文件");
  323. lvi.SubItems.Add(file.Length.ToString());
  324. lvi.SubItems.Add(file.LastAccessTime.ToString());
  325. FilesView.Items.Add(lvi);
  326. }
  327. }
  328. //FilesView_DoubleClick()
  329. private void FilesView_DoubleClick(object sender, System.EventArgs e)
  330. {
  331. try 
  332. {
  333. string FullName=FilesView.SelectedItems[0].Tag.ToString();
  334. if(FilesView.SelectedItems[0].ImageIndex==1) {
  335. return;
  336. }
  337. else {
  338. if(FilesView.SelectedItems[0].ImageIndex==2)
  339. System.Diagnostics.Process.Start(FullName);
  340. else 
  341. {
  342. txtCurPath.Text=FullName;
  343. FillFilesView(FullName);
  344. CurPath.Add(FullName);
  345. }
  346. }
  347. }
  348. catch(Exception ex) 
  349. {
  350. MessageBox.Show(ex.Message);
  351. }
  352. }
  353. //btnUp_Click()
  354. private void btnUp_Click(object sender, System.EventArgs e)
  355. {
  356. try 
  357. {
  358. if(CurPath.Count>1) 
  359. {
  360. //Current path is not root directory
  361. string FullName=CurPath[CurPath.Count-2].ToString();
  362. txtCurPath.Text=FullName;
  363. FillFilesView(FullName);
  364. CurPath.RemoveAt(CurPath.Count-1);
  365. }
  366. else 
  367. {
  368. if(CurPath.Count==1)
  369. CurPath.RemoveAt(CurPath.Count-1);
  370. this.txtCurPath.Text="My Computer";
  371. this.FilesView.Clear();
  372. this.FilesView.View= View.Details;
  373. this.FilesView.Columns.Add("本地磁盘",FilesView.Width/3,HorizontalAlignment.Left);
  374. //using the method "GetLogicalDrives()"
  375. string[] Drv= Directory.GetLogicalDrives();
  376. int DrvCnt= Drv.Length;
  377. //Display the disk
  378. for(int i=0;i<DrvCnt;i++) 
  379. {
  380. ListViewItem lvi= new ListViewItem();
  381. lvi.Text="驱动器"+Drv[i];
  382. lvi.ImageIndex=3;
  383. lvi.Tag=Drv[i];
  384. this.FilesView.Items.Add(lvi);
  385. }
  386. }
  387. }
  388. catch(Exception ex) 
  389. {
  390. MessageBox.Show(ex.Message);
  391. }
  392. }
  393. //Add a delete method
  394. private void DeleteMethod(object sender, System.EventArgs e) {
  395. try 
  396. {
  397. if(FilesView.SelectedItems.Count==0)
  398. return;
  399. //Delete the folder
  400. //test
  401. //MessageBox.Show(FilesView.SelectedItems[0].SubItems[0].Text);
  402. if(FilesView.SelectedItems[0].SubItems[1].Text=="文件夹") 
  403. {
  404. string strDir=FilesView.SelectedItems[0].Tag.ToString();
  405. DialogResult ret=MessageBox.Show("确定删除文件夹"+strDir+"?","确定删除",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
  406. if(ret==DialogResult.OK) 
  407. {
  408. Directory.Delete(strDir,false);
  409. string CurFullPath=CurPath[CurPath.Count-1];
  410. FillFilesView(CurFullPath);
  411. MessageBox.Show("文件夹: " +strDir+ "已成功删除" );
  412. }
  413. }
  414. else 
  415. {
  416. string strFile=FilesView.SelectedItems[0].Tag.ToString();
  417. DialogResult ret=MessageBox.Show("确定删除文件"+strFile+"?","确定删除",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
  418. if(ret==DialogResult.OK) 
  419. {
  420. File.Delete(strFile);
  421. string CurFullPath=CurPath[CurPath.Count-1];
  422. FillFilesView(CurFullPath);
  423. MessageBox.Show("文件: " +strFile+ "已成功删除" );
  424. }
  425. }
  426. }
  427. catch(Exception ex) 
  428. {
  429. MessageBox.Show(ex.Message);
  430. }
  431. }
  432. //Add a create folder method
  433. private void CreateFolderMethod(object sender, System.EventArgs e) {
  434. try 
  435. {
  436. NewFolder folderDlg= new NewFolder();
  437. folderDlg.lbParentPath.Text=CurPath[CurPath.Count-1];
  438. //Display the Dialog
  439. /*
  440. if(folderDlg.ShowDialog()==DialogResult.OK) {
  441. string CurFullPath=CurPath[CurPath.Count-1];
  442. FillFilesView(CurFullPath);
  443. }
  444. */
  445. folderDlg.ShowDialog();
  446. string CurFullPath=CurPath[CurPath.Count-1];
  447. FillFilesView(CurFullPath);
  448. //Dispose the Dialog
  449. folderDlg.Dispose();
  450. }
  451. catch(Exception ex) 
  452. {
  453. MessageBox.Show(ex.Message);
  454. }
  455. }
  456. //Add a create file method
  457. private void CreateFileMethod(object sender, System.EventArgs e) {
  458. try 
  459. {
  460. NewFile fileDlg= new NewFile();
  461. fileDlg.lbParentPath.Text=CurPath[CurPath.Count-1];
  462. //Display the Dialog
  463. /*
  464. if(fileDlg.ShowDialog()==DialogResult.OK) 
  465. {
  466. string CurFullPath=CurPath[CurPath.Count-1];
  467. //for test
  468. MessageBox.Show(CurFullPath);
  469. FillFilesView(CurFullPath);
  470. }
  471. */
  472. fileDlg.ShowDialog();
  473. string CurFullPath=CurPath[CurPath.Count-1];
  474. FillFilesView(CurFullPath);
  475. //Dispose the Dialog
  476. fileDlg.Dispose();
  477. }
  478. catch(Exception ex) 
  479. {
  480. MessageBox.Show(ex.Message);
  481. }
  482. }
  483. //method of menu
  484. private void DeleteMenu_Click(object sender, System.EventArgs e)
  485. {
  486. DeleteMethod(sender,e);
  487. }
  488. private void ContextmenuDelete_Click(object sender, System.EventArgs e)
  489. {
  490. DeleteMethod(sender, e);
  491. }
  492. private void NewFolderMenu_Click(object sender, System.EventArgs e)
  493. {
  494. CreateFolderMethod(sender, e);
  495. }
  496. private void NewFileMenu_Click(object sender, System.EventArgs e)
  497. {
  498. CreateFileMethod(sender, e);
  499. }
  500. private void ContextmenuNewFolder_Click(object sender, System.EventArgs e)
  501. {
  502. CreateFolderMethod(sender, e);
  503. }
  504. private void ContextmenuNewFile_Click(object sender, System.EventArgs e)
  505. {
  506. CreateFileMethod(sender, e);
  507. }
  508. private void ExitMenu_Click(object sender, System.EventArgs e)
  509. {
  510. this.Close();
  511. }
  512. //add for test
  513. //if the delete key is pressed, call the delete method
  514. private void FilesView_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  515. {
  516. if(e.KeyCode==Keys.Delete) {
  517. DeleteMethod(sender, e);
  518. }
  519. }
  520. private void FilesView_SelectedIndexChanged(object sender, System.EventArgs e)
  521. {
  522. }
  523. private void NewMenu_Click(object sender, System.EventArgs e)
  524. {
  525. }
  526. private void txtCurPath_TextChanged(object sender, System.EventArgs e)
  527. {
  528. }
  529. }
  530. }