📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace Wrox.ProCSharp.FilesAndRegistry
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private string currentFolderPath;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Button buttonDisplay;
private System.Windows.Forms.Button buttonUp;
private System.Windows.Forms.TextBox textBoxLastWriteTime;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBoxInput;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBoxCreationTime;
private System.Windows.Forms.ListBox listBoxFiles;
private System.Windows.Forms.ListBox listBoxFolders;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBoxLastAccessTime;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBoxFileSize;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox textBoxFileName;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBoxFolder;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.GroupBox groupBox5;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.TextBox textBoxNewPath;
private System.Windows.Forms.Button buttonDelete;
private System.Windows.Forms.Button buttonCopyTo;
private System.Windows.Forms.Button buttonMoveTo;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
protected void ClearAllFields()
{
listBoxFolders.Items.Clear();
listBoxFiles.Items.Clear();
textBoxFolder.Text = "";
textBoxFolder.Text = "";
textBoxFileName.Text = "";
textBoxCreationTime.Text = "";
textBoxLastAccessTime.Text = "";
textBoxLastWriteTime.Text = "";
textBoxFileSize.Text = "";
}
protected void DisplayFileInfo(string fileFullName)
{
FileInfo theFile = new FileInfo(fileFullName);
if (!theFile.Exists)
throw new FileNotFoundException("File not found: " + fileFullName);
textBoxFileName.Text = theFile.Name;
textBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString();
textBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString();
textBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString();
textBoxFileSize.Text = theFile.Length.ToString() + " bytes";
// enable move, copy, delete buttons
textBoxNewPath.Text = theFile.FullName;
textBoxNewPath.Enabled = true;
buttonCopyTo.Enabled = true;
buttonDelete.Enabled = true;
buttonMoveTo.Enabled = true;
}
protected void DisplayFolderList(string folderFullName)
{
DirectoryInfo theFolder = new DirectoryInfo(folderFullName);
if (!theFolder.Exists)
throw new DirectoryNotFoundException("Folder not found: "
+ folderFullName);
ClearAllFields();
DisableMoveFeatures();
textBoxFolder.Text = theFolder.FullName;
currentFolderPath = theFolder.FullName;
// list all subfolders in folder
foreach(DirectoryInfo nextFolder in theFolder.GetDirectories())
listBoxFolders.Items.Add(nextFolder.Name);
// list all files in folder
foreach(FileInfo nextFile in theFolder.GetFiles())
listBoxFiles.Items.Add(nextFile.Name);
}
protected void OnDisplayButtonClick(object sender, EventArgs e)
{
try
{
string folderPath = textBoxInput.Text;
DirectoryInfo theFolder = new DirectoryInfo(folderPath);
if (theFolder.Exists)
{
DisplayFolderList(theFolder.FullName);
return;
}
FileInfo theFile = new FileInfo(folderPath);
if (theFile.Exists)
{
DisplayFolderList(theFile.Directory.FullName);
int index = listBoxFiles.Items.IndexOf(theFile.Name);
listBoxFiles.SetSelected(index, true);
return;
}
throw new FileNotFoundException("There is no file or folder with "
+ "this name: " + textBoxInput.Text);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void DisableMoveFeatures()
{
textBoxNewPath.Text = "";
textBoxNewPath.Enabled = false;
buttonCopyTo.Enabled = false;
buttonDelete.Enabled = false;
buttonMoveTo.Enabled = false;
}
protected void OnListBoxFilesSelected(object sender, EventArgs e)
{
try
{
string selectedString = listBoxFiles.SelectedItem.ToString();
string fullFileName = Path.Combine(currentFolderPath, selectedString);
DisplayFileInfo(fullFileName);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
protected void OnListBoxFoldersSelected(object sender, EventArgs e)
{
try
{
string selectedString = listBoxFolders.SelectedItem.ToString();
string fullPathName = Path.Combine(currentFolderPath, selectedString);
DisplayFolderList(fullPathName);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
protected void OnUpButtonClick(object sender, EventArgs e)
{
try
{
string folderPath = new FileInfo(currentFolderPath).DirectoryName;
DisplayFolderList(folderPath);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
protected void OnDeleteButtonClick(object sender, EventArgs e)
{
try
{
string filePath = Path.Combine(currentFolderPath,
textBoxFileName.Text);
string query = "Really delete the file\n" + filePath + "?";
if (MessageBox.Show(query,
"Delete File?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
File.Delete(filePath);
DisplayFolderList(currentFolderPath);
}
}
catch(Exception ex)
{
MessageBox.Show("Unable to delete file. The following exception"
+ " occurred:\n" + ex.Message, "Failed");
}
}
protected void OnMoveButtonClick(object sender, EventArgs e)
{
try
{
string filePath = Path.Combine(currentFolderPath,
textBoxFileName.Text);
string query = "Really move the file\n" + filePath + "\nto "
+ textBoxNewPath.Text + "?";
if (MessageBox.Show(query,
"Move File?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
File.Move(filePath, textBoxNewPath.Text);
DisplayFolderList(currentFolderPath);
}
}
catch(Exception ex)
{
MessageBox.Show("Unable to move file. The following exception"
+ " occurred:\n" + ex.Message, "Failed");
}
}
protected void OnCopyButtonClick(object sender, EventArgs e)
{
try
{
string filePath = Path.Combine(currentFolderPath,
textBoxFileName.Text);
string query = "Really copy the file\n" + filePath + "\nto "
+ textBoxNewPath.Text + "?";
if (MessageBox.Show(query,
"Copy File?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
File.Copy(filePath, textBoxNewPath.Text);
DisplayFolderList(currentFolderPath);
}
}
catch(Exception ex)
{
MessageBox.Show("Unable to copy file. The following exception"
+ " occurred:\n" + ex.Message, "Failed");
}
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label8 = new System.Windows.Forms.Label();
this.buttonDisplay = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.textBoxLastWriteTime = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBoxInput = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBoxCreationTime = new System.Windows.Forms.TextBox();
this.listBoxFiles = new System.Windows.Forms.ListBox();
this.listBoxFolders = new System.Windows.Forms.ListBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox5 = new System.Windows.Forms.GroupBox();
this.label14 = new System.Windows.Forms.Label();
this.textBoxNewPath = new System.Windows.Forms.TextBox();
this.buttonDelete = new System.Windows.Forms.Button();
this.buttonCopyTo = new System.Windows.Forms.Button();
this.buttonMoveTo = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBoxFileSize = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.textBoxFileName = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBoxFolder = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBoxLastAccessTime = new System.Windows.Forms.TextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.label9 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label10 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label12 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label13 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox5 = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.groupBox5.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// label8
//
this.label8.Location = new System.Drawing.Point(256, 104);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(100, 16);
this.label8.TabIndex = 14;
this.label8.Text = "Folders";
//
// buttonDisplay
//
this.buttonDisplay.Location = new System.Drawing.Point(432, 24);
this.buttonDisplay.Name = "buttonDisplay";
this.buttonDisplay.TabIndex = 26;
this.buttonDisplay.Text = "Display";
this.buttonDisplay.Click += new System.EventHandler(this.OnDisplayButtonClick);
//
// buttonUp
//
this.buttonUp.Location = new System.Drawing.Point(408, 72);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(72, 24);
this.buttonUp.TabIndex = 24;
this.buttonUp.Text = "Up";
this.buttonUp.Click += new System.EventHandler(this.OnUpButtonClick);
//
// textBoxLastWriteTime
//
this.textBoxLastWriteTime.Enabled = false;
this.textBoxLastWriteTime.Location = new System.Drawing.Point(16, 112);
this.textBoxLastWriteTime.Name = "textBoxLastWriteTime";
this.textBoxLastWriteTime.Size = new System.Drawing.Size(184, 20);
this.textBoxLastWriteTime.TabIndex = 16;
this.textBoxLastWriteTime.Text = "";
//
// label2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -