以下是一个示例代码:

using System;
using System.IO;

namespace RandomFileCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFolder = @"C:\SourceFolder"; // 源文件夹路径
            string destinationFolder = @"C:\DestinationFolder"; // 目标文件夹路径
            Random random = new Random();
            string[] files = Directory.GetFiles(sourceFolder); // 获取源文件夹中的所有文件

            if (files.Length > 0)
            {
                int randomNumber = random.Next(files.Length); // 生成随机数
                string randomFile = files[randomNumber]; // 获取随机文件的路径

                // 复制文件到目标文件夹
                string fileName = Path.GetFileName(randomFile); // 获取文件名
                string newFilePath = Path.Combine(destinationFolder, fileName); // 组合目标文件路径
                File.Copy(randomFile, newFilePath); // 复制文件

                // 删除源文件
                File.Delete(randomFile);

                Console.WriteLine("文件复制完成并删除成功!");
            }
            else
            {
                Console.WriteLine("源文件夹中没有文件!");
            }
        }
    }
}

请注意,此代码仅供参考,并且可能需要根据特定的需求进行修改和优化。


原文地址: https://www.cveoy.top/t/topic/b7xu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录