📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FileManager
{
public partial class Form1 : Form
{
private bool mIsInitialized = false;
public Form1()
{
InitializeComponent();
Initialize();
}
private void Initialize()
{
try
{
if (mIsInitialized)
return;
this.tb_CurPath.Text = GetInitDir();
lstFiles.View = View.Details;
lstFiles.Columns.Add("文件名称", lstFiles.Width / 3, HorizontalAlignment.Left);
lstFiles.Columns.Add("目录/文件", lstFiles.Width / 6, HorizontalAlignment.Center);
lstFiles.Columns.Add("文件大小", lstFiles.Width / 6, HorizontalAlignment.Right);
lstFiles.Columns.Add("最后存取时间", lstFiles.Width / 3, HorizontalAlignment.Left);
FillFiles();
mIsInitialized = true;
}
catch (System.Exception E)
{
ErrorHandler(E.ToString());
}
}
private string GetInitDir()
{
// 返回应用执行时候的当前目录
return Application.StartupPath;
}
private void ErrorHandler(string ErrorDescription)
{
MessageBox.Show(ErrorDescription);
}
private void FillFiles()
{
// 将文件列表写入ListView
lstFiles.Items.Clear();
DirectoryInfo cd = new DirectoryInfo(this.tb_CurPath.Text + "\\");
foreach (DirectoryInfo d in cd.GetDirectories())
{
lstFiles.Items.Add(new ListViewItem(new string[] { d.Name, "目录", "", d.LastWriteTime.ToString("yyyy-MM-dd hh:mm:ss", null) }));
}
foreach (FileInfo f in cd.GetFiles())
{
lstFiles.Items.Add(new ListViewItem(new string[] { f.Name, "档案", f.Length.ToString(), f.LastWriteTime.ToString("yyyy-MM-dd hh:mm:ss", null) }));
}
}
private void btn_Up_Click(object sender, EventArgs e)
{
if (this.tb_CurPath.Text.LastIndexOf("\\") != -1)
{
this.tb_CurPath.Text = this.tb_CurPath.Text.Substring(0, this.tb_CurPath.Text.LastIndexOf("\\"));
FillFiles();
}
}
private void btn_CreateFolder_Click(object sender, EventArgs e)
{
if (txtNewDir.Text == "")
{
MessageBox.Show("目录名称不可空白");
return;
}
if (Directory.Exists(tb_CurPath.Text + "\\" + txtNewDir.Text))
{
MessageBox.Show("目录[" + tb_CurPath.Text + "\\" + txtNewDir.Text + "]已存在");
return;
}
Directory.CreateDirectory(tb_CurPath.Text + "\\" + txtNewDir.Text);
FillFiles();
}
private void btn_CreateFile_Click(object sender, EventArgs e)
{
if (txtNewFile.Text == "")
{
MessageBox.Show("文件名称不可空白");
return;
}
if (File.Exists(tb_CurPath.Text + "\\" + txtNewFile.Text))
{
MessageBox.Show("文件[" + tb_CurPath.Text + "\\" + txtNewFile.Text + "]已存在");
return;
}
FileStream fs = File.Create(txtCurDir.Text + "\\" + txtNewFile.Text);
fs.Close();
FillFiles();
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (lstFiles.SelectedItems.Count == 0)
return;
if (lstFiles.SelectedItems[0].SubItems[1].Text == "目录")//删除目录
{
string strDir = tb_CurPath.Text + "\\" + lstFiles.SelectedItems[0].Text;
DialogResult ret = MessageBox.Show("确定删除目录[" + strDir + "]?", "确定删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (ret == DialogResult.OK)
{
Directory.Delete(strDir, false);
FillFiles();
MessageBox.Show("目录[" + strDir + "]已成功的删除.");
}
}
else //删除档案
{
string strFile = tb_CurPath.Text + "\\" + lstFiles.SelectedItems[0].Text;
DialogResult ret = MessageBox.Show("确定删除档案[" + strFile + "]?", "确定删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (ret == DialogResult.OK)
{
File.Delete(strFile);
FillFiles();
MessageBox.Show("档案[" + strFile + "]已成功的删除.");
}
}
}
catch (System.Exception E)
{
ErrorHandler(E.ToString());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -