我想用C#编一个小程序,定期删除文件和复制文件,

时间:2022-09-14 22:26:20
我想用C#编一个小程序,定期删除文件和复制文件,最好可以能够选择要删除的文件或者文件夹  请问大神们这个程序怎么弄啊 
winform

5 个解决方案

#1


1. 程序主动:设置定时器,定时器触发你要做的操作
2. windows触发:用windows的事件 可以按时间设置来 启动你的程序,程序执行完成自动关闭,等系统下次触发

#2


如果是工作上需要的话,还是用现成的工具。练习练习是可以的

#3


给你写了一个,将文件夹abc下文件先复制到abc1,然后删除

private void Form1_Load(object sender, EventArgs e)
{
    //启动时钟,10秒后删除文件
    System.Timers.Timer t = new System.Timers.Timer(10000);
    t.Elapsed += new System.Timers.ElapsedEventHandler(delFile);
    t.Enabled = true;

}

protected void delFile(object sender, System.Timers.ElapsedEventArgs e)
{
    string path = @"F:\\abc";
    string path_copy=@"F:\\abc1";

    if(!Directory.Exists(path))
    {
        MessageBox.Show("目录不存在,请重新选择");
        return;

    }

    DirectoryInfo dir = new DirectoryInfo(path);
    FileInfo[] files = dir.GetFiles();

    try
    {
        foreach (FileInfo fi in files)
        {
            File.Copy(fi.FullName, path_copy + "\\" + fi.Name,false);
            File.Delete(fi.FullName);
        }
        MessageBox.Show("删除文件成功!");

    }
    catch
    {
        MessageBox.Show("删除文件失败!");
    }
    
}



测试通过

#4


引用 3 楼 From_* 的回复:
给你写了一个,将文件夹abc下文件先复制到abc1,然后删除

private void Form1_Load(object sender, EventArgs e)
{
    //启动时钟,10秒后删除文件
    System.Timers.Timer t = new System.Timers.Timer(10000);
    t.Elapsed += new System.Timers.ElapsedEventHandler(delFile);
    t.Enabled = true;

}

protected void delFile(object sender, System.Timers.ElapsedEventArgs e)
{
    string path = @"F:\\abc";
    string path_copy=@"F:\\abc1";

    if(!Directory.Exists(path))
    {
        MessageBox.Show("目录不存在,请重新选择");
        return;

    }

    DirectoryInfo dir = new DirectoryInfo(path);
    FileInfo[] files = dir.GetFiles();

    try
    {
        foreach (FileInfo fi in files)
        {
            File.Copy(fi.FullName, path_copy + "\\" + fi.Name,false);
            File.Delete(fi.FullName);
        }
        MessageBox.Show("删除文件成功!");

    }
    catch
    {
        MessageBox.Show("删除文件失败!");
    }
    
}



测试通过

顶一个

#5


不用那么麻烦。 写个脚本,用windows task scheduler定期执行。这两者结合实现的功能顶上一个程序员写半年的。

#1


1. 程序主动:设置定时器,定时器触发你要做的操作
2. windows触发:用windows的事件 可以按时间设置来 启动你的程序,程序执行完成自动关闭,等系统下次触发

#2


如果是工作上需要的话,还是用现成的工具。练习练习是可以的

#3


给你写了一个,将文件夹abc下文件先复制到abc1,然后删除

private void Form1_Load(object sender, EventArgs e)
{
    //启动时钟,10秒后删除文件
    System.Timers.Timer t = new System.Timers.Timer(10000);
    t.Elapsed += new System.Timers.ElapsedEventHandler(delFile);
    t.Enabled = true;

}

protected void delFile(object sender, System.Timers.ElapsedEventArgs e)
{
    string path = @"F:\\abc";
    string path_copy=@"F:\\abc1";

    if(!Directory.Exists(path))
    {
        MessageBox.Show("目录不存在,请重新选择");
        return;

    }

    DirectoryInfo dir = new DirectoryInfo(path);
    FileInfo[] files = dir.GetFiles();

    try
    {
        foreach (FileInfo fi in files)
        {
            File.Copy(fi.FullName, path_copy + "\\" + fi.Name,false);
            File.Delete(fi.FullName);
        }
        MessageBox.Show("删除文件成功!");

    }
    catch
    {
        MessageBox.Show("删除文件失败!");
    }
    
}



测试通过

#4


引用 3 楼 From_* 的回复:
给你写了一个,将文件夹abc下文件先复制到abc1,然后删除

private void Form1_Load(object sender, EventArgs e)
{
    //启动时钟,10秒后删除文件
    System.Timers.Timer t = new System.Timers.Timer(10000);
    t.Elapsed += new System.Timers.ElapsedEventHandler(delFile);
    t.Enabled = true;

}

protected void delFile(object sender, System.Timers.ElapsedEventArgs e)
{
    string path = @"F:\\abc";
    string path_copy=@"F:\\abc1";

    if(!Directory.Exists(path))
    {
        MessageBox.Show("目录不存在,请重新选择");
        return;

    }

    DirectoryInfo dir = new DirectoryInfo(path);
    FileInfo[] files = dir.GetFiles();

    try
    {
        foreach (FileInfo fi in files)
        {
            File.Copy(fi.FullName, path_copy + "\\" + fi.Name,false);
            File.Delete(fi.FullName);
        }
        MessageBox.Show("删除文件成功!");

    }
    catch
    {
        MessageBox.Show("删除文件失败!");
    }
    
}



测试通过

顶一个

#5


不用那么麻烦。 写个脚本,用windows task scheduler定期执行。这两者结合实现的功能顶上一个程序员写半年的。