form1.cs
来自「Sams Teach Yourself C# Web Programming i」· CS 代码 · 共 324 行
CS
324 行
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Manipulating_Files2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class fclsManipulatingFiles : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtSource;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button btnOpenFile;
private System.Windows.Forms.TextBox txtDestination;
private System.Windows.Forms.Button btnSaveFile;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Button btnCopyFile;
private System.Windows.Forms.Button btnMove;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnGetFileProperties;
private System.Windows.Forms.TextBox txtProperties;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public fclsManipulatingFiles()
{
//
// 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 );
}
#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.txtSource = new System.Windows.Forms.TextBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.btnOpenFile = new System.Windows.Forms.Button();
this.txtDestination = new System.Windows.Forms.TextBox();
this.btnSaveFile = new System.Windows.Forms.Button();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.btnCopyFile = new System.Windows.Forms.Button();
this.btnMove = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.btnGetFileProperties = new System.Windows.Forms.Button();
this.txtProperties = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtSource
//
this.txtSource.Location = new System.Drawing.Point(95, 8);
this.txtSource.Name = "txtSource";
this.txtSource.Size = new System.Drawing.Size(184, 20);
this.txtSource.TabIndex = 0;
this.txtSource.Text = "";
//
// btnOpenFile
//
this.btnOpenFile.Location = new System.Drawing.Point(8, 8);
this.btnOpenFile.Name = "btnOpenFile";
this.btnOpenFile.Size = new System.Drawing.Size(80, 23);
this.btnOpenFile.TabIndex = 1;
this.btnOpenFile.Text = "Source";
this.btnOpenFile.Click += new System.EventHandler(this.btnOpenFile_Click);
//
// txtDestination
//
this.txtDestination.Location = new System.Drawing.Point(95, 40);
this.txtDestination.Name = "txtDestination";
this.txtDestination.Size = new System.Drawing.Size(184, 20);
this.txtDestination.TabIndex = 2;
this.txtDestination.Text = "";
//
// btnSaveFile
//
this.btnSaveFile.Location = new System.Drawing.Point(8, 40);
this.btnSaveFile.Name = "btnSaveFile";
this.btnSaveFile.Size = new System.Drawing.Size(80, 23);
this.btnSaveFile.TabIndex = 3;
this.btnSaveFile.Text = "Destination";
this.btnSaveFile.Click += new System.EventHandler(this.btnSaveFile_Click);
//
// saveFileDialog1
//
this.saveFileDialog1.FileName = "doc1";
//
// btnCopyFile
//
this.btnCopyFile.Location = new System.Drawing.Point(96, 80);
this.btnCopyFile.Name = "btnCopyFile";
this.btnCopyFile.TabIndex = 4;
this.btnCopyFile.Text = "Copy";
this.btnCopyFile.Click += new System.EventHandler(this.btnCopyFile_Click);
//
// btnMove
//
this.btnMove.Location = new System.Drawing.Point(96, 112);
this.btnMove.Name = "btnMove";
this.btnMove.TabIndex = 5;
this.btnMove.Text = "Move";
this.btnMove.Click += new System.EventHandler(this.btnMove_Click);
//
// btnDelete
//
this.btnDelete.Location = new System.Drawing.Point(96, 144);
this.btnDelete.Name = "btnDelete";
this.btnDelete.TabIndex = 6;
this.btnDelete.Text = "Delete";
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnGetFileProperties
//
this.btnGetFileProperties.Location = new System.Drawing.Point(8, 176);
this.btnGetFileProperties.Name = "btnGetFileProperties";
this.btnGetFileProperties.Size = new System.Drawing.Size(80, 56);
this.btnGetFileProperties.TabIndex = 7;
this.btnGetFileProperties.Text = "Get Properties of Source File";
this.btnGetFileProperties.Click += new System.EventHandler(this.btnGetFileProperties_Click);
//
// txtProperties
//
this.txtProperties.Location = new System.Drawing.Point(96, 176);
this.txtProperties.Multiline = true;
this.txtProperties.Name = "txtProperties";
this.txtProperties.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtProperties.Size = new System.Drawing.Size(184, 88);
this.txtProperties.TabIndex = 8;
this.txtProperties.Text = "";
//
// fclsManipulatingFiles
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.txtProperties,
this.btnGetFileProperties,
this.btnDelete,
this.btnMove,
this.btnCopyFile,
this.btnSaveFile,
this.txtDestination,
this.btnOpenFile,
this.txtSource});
this.Name = "fclsManipulatingFiles";
this.Text = "Manipulating Files";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new fclsManipulatingFiles());
}
private void btnOpenFile_Click(object sender, System.EventArgs e)
{
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Select a File";
openFileDialog1.Filter = "Text Files|*.txt";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
txtSource.Text = openFileDialog1.FileName;
else
txtSource.Text = "";
}
private void btnSaveFile_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Title = "Specify Destination Filename";
saveFileDialog1.Filter = "Text Files|*.txt";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.OverwritePrompt = true;
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
txtDestination.Text = saveFileDialog1.FileName;
}
bool SourceFileExists()
{
if (!System.IO.File.Exists(txtSource.Text))
{
MessageBox.Show("The source file does not exist!");
return false;
}
else
return true;
}
private void btnCopyFile_Click(object sender, System.EventArgs e)
{
if (!SourceFileExists()) return;
System.IO.File.Copy(txtSource.Text, txtDestination.Text);
MessageBox.Show("The file has been successfully copied.");
}
private void btnMove_Click(object sender, System.EventArgs e)
{
if (!SourceFileExists()) return;
System.IO.File.Move(txtSource.Text, txtDestination.Text);
MessageBox.Show("The file has been successfully moved.");
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
if (!SourceFileExists()) return;
if (MessageBox.Show("Are you sure you want to delete the source file?", "Delete Verification",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
System.IO.File.Delete(txtSource.Text);
MessageBox.Show("The file has been successfully deleted.");
System.IO.FileAttributes fileAttributes;
}
}
private void btnGetFileProperties_Click(object sender, System.EventArgs e)
{
System.Text.StringBuilder stbProperties = new System.Text.StringBuilder("");
System.IO.FileAttributes fileAttributes ;
if (!SourceFileExists()) return;
// Get the dates.
stbProperties.Append("Created: ");
stbProperties.Append(System.IO.File.GetCreationTime(txtSource.Text));
stbProperties.Append("\r\n");
stbProperties.Append("Accessed: ");
stbProperties.Append(System.IO.File.GetLastAccessTime(txtSource.Text));
stbProperties.Append("\r\n");
stbProperties.Append("Modified: ");
stbProperties.Append(System.IO.File.GetLastWriteTime(txtSource.Text));
// Get File Attributes
fileAttributes = System.IO.File.GetAttributes(txtSource.Text);
stbProperties.Append("\r\n");
stbProperties.Append("Normal: ");
stbProperties.Append(
Convert.ToBoolean((fileAttributes & System.IO.FileAttributes.Normal)
==System.IO.FileAttributes.Normal));
stbProperties.Append("\r\n");
stbProperties.Append("Hidden: ");
stbProperties.Append(
Convert.ToBoolean((fileAttributes & System.IO.FileAttributes.Hidden)
==System.IO.FileAttributes.Hidden));
stbProperties.Append("\r\n");
stbProperties.Append("ReadOnly: ");
stbProperties.Append(
Convert.ToBoolean((fileAttributes & System.IO.FileAttributes.ReadOnly)
==System.IO.FileAttributes.ReadOnly));
stbProperties.Append("\r\n");
stbProperties.Append("System: ");
stbProperties.Append(
Convert.ToBoolean((fileAttributes & System.IO.FileAttributes.System)
==System.IO.FileAttributes.System));
stbProperties.Append("\r\n");
stbProperties.Append("Temporary File: ");
stbProperties.Append(
Convert.ToBoolean((fileAttributes & System.IO.FileAttributes.Temporary)
==System.IO.FileAttributes.Temporary));
stbProperties.Append("\r\n");
stbProperties.Append("Archive: ");
stbProperties.Append(
Convert.ToBoolean((fileAttributes & System.IO.FileAttributes.Archive)
==System.IO.FileAttributes.Archive));
txtProperties.Text = stbProperties.ToString();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?