C# Create Windows Forms Application Instance 11 [File Management]

All instance 11? Don't you create a project yet? Don't know the steps? Can't write code? Please visit the link below to learn!

C# Create Windows Forms Application Instance 1

C# Create Windows Forms Application Instance 2

1. Knowledge points

2. Style Design

Select the following  controls in the tool

 

 

 

 

 

3. Code implementation 

using System.Windows.Forms;
using System.IO;

namespace 文件管理
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        //”删除”的Click事件的代码
        private void button1_Click(object sender, EventArgs e)
        {
            string dpath = textBox1.Text;
            if (dpath != "")
            {
                if (File.Exists(@dpath))     //如果路径下存在文件
                {
                    File.Delete(@dpath);
                    MessageBox.Show(@dpath + "删除成功!");
                }
                else
                { MessageBox.Show("文件不存在!"); }
            }
            else
            {
                MessageBox.Show("请输入要删除的文件!");
                textBox1.Focus();
            }
        }
        //”复制”的Click事件的代码
        private void button2_Click(object sender, EventArgs e)
        {
            string srcpath = textBox2.Text;
            string destpath = textBox3.Text;
            if (srcpath == "" || destpath == "")
                MessageBox.Show("请输入要复制的源文件与目的文件路径!");
            else
            {
                if (File.Exists(@srcpath))
                {
                    File.Copy(@srcpath, @destpath);
                    MessageBox.Show(@srcpath + "复制到" + @destpath + "成功!");
                }
                else
                { MessageBox.Show("源文件不存在!"); }
            }
        }
        //”移动”的Click事件的代码
        private void button3_Click(object sender, EventArgs e)
        {
            string srcpath = textBox4.Text;
            string destpath = textBox5.Text;
            if (srcpath == "" || destpath == "")
                MessageBox.Show("请输入要移动的源文件与目的文件路径!");
            else
            {
                if (File.Exists(@srcpath))
                {
                    File.Move(@srcpath, @destpath);
                    MessageBox.Show(@srcpath + "移动到" + @destpath + "成功!");
                }
                else
                { MessageBox.Show("源文件不存在!"); }
            }
        }
        //”获取信息”的Click事件的代码
        private void button4_Click(object sender, EventArgs e)
        {
            string detailpath = textBox6.Text;
            richTextBox1.Clear();
            if (File.Exists(@detailpath))
            {
                FileInfo fi = new FileInfo(@detailpath);
                if ((fi.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                    richTextBox1.AppendText("文件属性:隐藏文件\n");
                else
                    richTextBox1.AppendText("文件属性:不是隐藏文件\n");
                richTextBox1.AppendText("文件建立时间:" + fi.CreationTime + '\n');
                richTextBox1.AppendText("文件最后修改时间:" + fi.LastWriteTime + '\n');
                richTextBox1.AppendText("文件长度:" + fi.Length + '\n');
            }
            else
                MessageBox.Show("文件不存在!");
        }
    }
}

4. Execution effect 

Guess you like

Origin blog.csdn.net/qq_45037155/article/details/124315081