单文件和多文件移动

文章展示了C#中如何使用事件处理实现文件操作,包括单个文件的移动和多文件选择时的逐个移动,涉及路径管理、文件名处理以及目录创建等技术。
摘要由CSDN通过智能技术生成
//单文件
private void button1_Click(object sender, EventArgs e)
        {
            string newPath = "";
            string path = @"E:\test\novel";
            string oldPath = "";
            string Suffix = "";
            newPath = path + @"\" + comboBox1.Text + @"\" + textBox1.Text;
            CreateDirectory(newPath);
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                oldPath = dialog.FileName;
                Suffix = oldPath.Substring(oldPath.IndexOf('.') + 1, oldPath.Length - (oldPath.IndexOf('.') + 1));
            }
            MoveFile(oldPath, newPath + @"\" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + Suffix);
        }
//多文件
private void button2_Click(object sender, EventArgs e)
        {
            string newPath = "";
            string path = @"E:\test\novel";
            string oldPath = "";
            string Suffix = "";
            newPath = path + @"\" + comboBox2.Text + @"\" + textBox2.Text;
            CreateDirectory(newPath);
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                int i = 0;
                foreach (string file in dialog.FileNames)
                {
                    oldPath = file;
                    Suffix = oldPath.Substring(oldPath.IndexOf('.') + 1, oldPath.Length - (oldPath.IndexOf('.') + 1));
                    MoveFile(oldPath, newPath + @"\" + DateTime.Now.ToString("yyyyMMddHHmmss") + i.ToString() + "." + Suffix);
                    i++;
                }
            }
        }
public void CreateDirectory(string Path)
        {
            Directory.CreateDirectory(Path);
        }

public void MoveFile(string oldPath,string newPath)
        {
            File.Move(oldPath, newPath);
        }
  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值