📄 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 FileManage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnnew_Click(object sender, EventArgs e)
{
//显示保存文件对话框
if (this.savefile.ShowDialog() == DialogResult.OK)
{
//获取文件名
this.path = savefile.FileName;
//如果目标文件存在,在删除原有文件
if (File.Exists(this.path)) File.Delete(this.path);
{
//创建文件
using (StreamWriter sw = File.CreateText(path))
{
//写入数据
sw.WriteLine("This is my text file!");
sw.Close();
}
//读取文件内容,并显示在窗体的txtfilecon控件中
StreamReader sr = new StreamReader(path);
txtfilecon.Text = (sr.ReadToEnd());
sr.Close();
}
}
}
private void btnappend_Click(object sender, EventArgs e)
{
//判断要追加的内容是否为空
if (textappend.Text != "")
{
StreamWriter swadd = File.AppendText(path);
//追加内容
swadd.WriteLine(textappend.Text);
swadd.Flush();
swadd.Close();
textappend.Text = "";
StreamReader srshow = new StreamReader(path);
//显示
txtfilecon.Text = (srshow.ReadToEnd());
srshow.Close();
}
}
private void btncopy_Click(object sender, EventArgs e)
{
openfile.FileName = null;
//显示打开文件对话框
if (openfile.ShowDialog() == DialogResult.OK)
{
//获取目标文件
string firstpath = openfile.FileName;
savefile.FileName = "";
//显示保存文件对话框
if (savefile.ShowDialog() == DialogResult.OK)
{
//获取新文件路径
string newpath = savefile.FileName;
if (File.Exists(newpath))
File.Delete(newpath);
//拷贝文件
File.Copy(firstpath, newpath, true);
MessageBox.Show("复制成功!");
}
}
}
private void btnmove_Click(object sender, EventArgs e)
{
openfile.FileName = null;
if (openfile.ShowDialog() == DialogResult.OK)
{
string firstfile = openfile.FileName;
savefile.FileName = firstfile;
if (savefile.ShowDialog() == DialogResult.OK)
{
string newfile = savefile.FileName;
File.Move(firstfile, newfile);
MessageBox.Show("移动成功!");
}
}
}
private void btninfo_Click(object sender, EventArgs e)
{
openfile.FileName = null;
//显示打开文件对话框
if (openfile.ShowDialog() == DialogResult.OK)
{
//获取文件
string newpath = openfile.FileName;
//获取文件信息
FileInfo file = new FileInfo(newpath);
string creattime = file.CreationTime.ToString();
string lastwritetime = file.LastWriteTime.ToString();
string lastaccesstime = file.LastAccessTime.ToString();
//输出文件信息
MessageBox.Show("创建日期:" + creattime + "\n上次修改时间:" + lastwritetime + "\n上次读取时间:" + lastaccesstime);
}
}
private void btndelete_Click(object sender, EventArgs e)
{
openfile.FileName = null;
//显示打开文件对话框
if (openfile.ShowDialog() == DialogResult.OK)
{
//获取目标文件
string newpath = openfile.FileName;
//如果目标文件存在,则删除该文件
if (File.Exists(newpath))
{
File.Delete(newpath);
MessageBox.Show("删除成功!");
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -