二、DEMO c#移动复制文件到另外一个文件夹

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;

namespace copyImage
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }


        protected override void OnStart(string[] args)
        {
            EventLog.WriteEntry("我的服务启动");


            System.Timers.Timer t = new System.Timers.Timer();
            t.Interval = 5000;
            t.Elapsed += new System.Timers.ElapsedEventHandler(Timer1_Elapsed);
            t.AutoReset = true;
            t.Enabled = true;


        }


        protected override void OnStop()
        {
        }




        private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            // 得到 hour minute second  如果等于某个值就开始执行某个程序。  
           // int intHour = e.SignalTime.Hour;
          //  int intMinute = e.SignalTime.Minute;
         //   int intSecond = e.SignalTime.Second;
          //  // 定制时间; 比如 在10:30 :00 的时候执行某个函数  
          //  int iSecond = 00;
            // 设置  每秒钟的开始执行一次  
          //  if (intSecond == iSecond)
          //  {              
                // Console.WriteLine("每秒钟的开始执行一次!");
            CopyDir(@"C:\Users\user\Desktop\rxtx-2.1-7-bins-r2", @"C:\Users\user\Desktop\caffe");
           // }
        }






        public static void CopyDir(string sourcePath, string destPath)
        {
            if (Directory.Exists(sourcePath))
            {
                if (!Directory.Exists(destPath))
                {
                    //目标目录不存在则创建  
                    try
                    {
                        Directory.CreateDirectory(destPath);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("创建目标目录失败:" + ex.Message);
                    }
                }
                //获得源文件下所有文件  
                List files = new List(Directory.GetFiles(sourcePath));
                files.ForEach(c =>
                {
                    string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
                    //覆盖模式  
                    if (File.Exists(destFile))
                    {
                        File.Delete(destFile);
                    }
                    File.Move(c, destFile);
                });
                //获得源文件下所有目录文件  
                List folders = new List(Directory.GetDirectories(sourcePath));


                folders.ForEach(c =>
                {
                    string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
                    //Directory.Move必须要在同一个根目录下移动才有效,不能在不同卷中移动。  
                    //Directory.Move(c, destDir);  


                    //采用递归的方法实现  
                    CopyDir(c, destDir);
                });




                DelectDir(sourcePath);




            }               
            else
            {
                throw new DirectoryNotFoundException("源目录不存在!");
            }  
        }




        public static void DelectDir(string srcPath)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)            //判断是否文件夹
                    {
                        DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                        subdir.Delete(true);          //删除子目录和文件
                    }
                    else
                    {
                        File.Delete(i.FullName);      //删除指定文件
                    }
                }
            }
            catch (Exception )
            {
                throw;
            }
        }


    }
}


二。获取配置文件的几行代码


 var reader = new AppSettingsReader();


                var src = reader.GetValue("src", typeof(string));
                var destination = reader.GetValue("destination", typeof(string));
                //Console.WriteLine("String setting: " + stringSetting);


                //var dateTimeSetting = reader.GetValue("DateTime setting", typeof(DateTime));
                //Console.WriteLine("DateTime setting: " + dateTimeSetting);
               // Console.WriteLine("每秒钟的开始执行一次!");
                CopyDir(@"" + src, @"" + destination);  

猜你喜欢

转载自blog.csdn.net/weixin_37986381/article/details/80786897