Пример #1
0
        /*
         * 获得指定路径下所有文件名
         * string path  文件路径
         */
        public static void getFileName(string path)
        {
            DirectoryInfo root = new DirectoryInfo(path);

            foreach (FileInfo f in root.GetFiles())
            {
                string newcopypath = copyPath + "\\" + path.Substring(3, path.Length - 3);//创建新的文件夹目录
                if (!Directory.Exists(newcopypath))
                {
                    Directory.CreateDirectory(newcopypath);
                }
                newcopypath = newcopypath + "\\" + f.Name;//生成新的文件
                if (!File.Exists(newcopypath))
                {
                    try
                    {
                        f.CopyTo(newcopypath);
                        successCount += 1;
                        totalCount   += 1;
                        LogerHelper.WriteOperateLog(string.Format("拷贝成功,成功个数:{0},原路径:{1} => 存储新路径:{2}", successCount, f.FullName, newcopypath));
                    }
                    catch (Exception ex)
                    {
                        totalCount += 1;
                        LogerHelper.WriteOperateLog(string.Format("拷贝失败,失败个数:{0},原路径:{1} => 存储新路径:{2},错误信息:{3}", totalCount - successCount, f.FullName, newcopypath, ex.Message));
                        continue;
                    }
                }
            }
        }
Пример #2
0
        private static void OnFileChanged(Object source, FileSystemEventArgs e)
        {
            //采用临时禁用法解决触发两次事件的问题,只适合解决监控单个文件的处理
            if (source != null)
            {
                var watcher = source as FileSystemWatcher;
                watcher.EnableRaisingEvents = false;

                Thread th = new Thread(new ThreadStart(
                                           delegate()
                {
                    Thread.Sleep(1000);
                    watcher.EnableRaisingEvents = true;
                }
                                           ));
                th.Start();
                IsRestart = true;
            }
            LogerHelper.WriteOperateLog(string.Format("修改类型:{0},配置文件{0}被修改", e.ChangeType, e.FullPath));
            LogerHelper.WriteOperateLog("是否重启服务:" + IsRestart);
            if (IsRestart)
            {
                //重启服务
                ICommand command = new RestartCommand();
                command.DealCommand();
                IsRestart = false;
            }
        }
Пример #3
0
 /// <summary>
 /// 监听服务接收的指令信息
 /// </summary>
 public static void FileMonitoringCommand(FileSystemWatcher curWatcher)
 {
     //FileStream fs = new FileStream(@"E:\log.txt", FileMode.OpenOrCreate, FileAccess.Write);
     //StreamWriter sw = new StreamWriter(fs);
     //sw.BaseStream.Seek(0, SeekOrigin.End);
     //sw.WriteLine("WindowsService: Service Started" + DateTime.Now.ToString() + AppDomain.CurrentDomain.BaseDirectory + "\n");
     //sw.Flush();
     //sw.Close();
     //fs.Close();
     try
     {
         curWatcher = new FileSystemWatcher();
         curWatcher.BeginInit();
         curWatcher.Filter = "App.config";//只监视config文件的修改动作(例:fsw.Filter = "*.txt|*.doc|*.jpg")
         curWatcher.IncludeSubdirectories = false;
         curWatcher.Path = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");
         LogerHelper.WriteOperateLog("监控文件路径:" + curWatcher.Path);
         curWatcher.Changed            += new FileSystemEventHandler(OnFileChanged);
         curWatcher.EnableRaisingEvents = true;//开启监控
         curWatcher.EndInit();
     }
     catch (Exception ex)
     {
         LogerHelper.WriteErrorLog(ex);
     }
 }
Пример #4
0
 /// <summary>
 /// 监听指令
 /// </summary>
 private static void MiniCommand()
 {
     try
     {
         var time = 2;
         int.TryParse(ConfigurationManager.AppSettings["ReadTime"], out time);
         while (!IsStoped)
         {
             try
             {
                 FileCopyWorker worker = new FileCopyWorker();
                 worker.Run();
             }
             catch (Exception e)
             {
                 LogerHelper.WriteErrorLog(e);
             }
             if (threadCommand.ThreadState == ThreadState.Running)
             {
                 Thread.Sleep(time * 1000);
             }
         }
     }
     catch (Exception e)
     {
         LogerHelper.WriteErrorLog(e);
     }
 }
Пример #5
0
        /// <summary>
        /// 监听服务状态
        /// </summary>
        private static void MiniServiceStatus()
        {
            var time = 5;

            int.TryParse(ConfigurationManager.AppSettings["IntervalTime"], out time);
            string serviceName = ConfigurationManager.AppSettings["ServiceName"];

            while (true)
            {
                try
                {
                    ServiceController sc = new ServiceController(serviceName);
                    if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
                        (sc.Status.Equals(ServiceControllerStatus.StopPending)))
                    {
                        sc.Start();
                        string msg = string.Format("{1}:服务{0}发生异常,开始重新启动!", serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        LogerHelper.WriteOperateLog(msg);
                    }
                    sc.Refresh();
                }
                catch (Exception ex)
                {
                    LogerHelper.WriteErrorLog(ex);
                }
                if (thread.ThreadState == ThreadState.Running)
                {
                    Thread.Sleep(time * 1000);
                }
            }
        }
Пример #6
0
        /// <summary>
        /// 重启服务
        /// </summary>
        public static void RestartService()
        {
            var    serviceName = ConfigurationManager.AppSettings["ServiceName"];
            string msg         = string.Format("{1}:服务{0}发生异常,开始重新启动!", serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            LogerHelper.WriteOperateLog(msg);
            //停止服务
            StopService();
            //启动服务
            StartService(serviceName);
        }
Пример #7
0
 /// <summary>
 /// 启动某个服务
 /// </summary>
 /// <param name="serviceName"></param>
 private static void StartService(string serviceName)
 {
     try
     {
         ServiceController[] services = ServiceController.GetServices();
         foreach (ServiceController service in services)
         {
             if (service.ServiceName == serviceName)
             {
                 service.Start();
                 service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));//等待服务运行
             }
         }
         string msg = string.Format("{1}:服务{0}启动完成!", serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
         LogerHelper.WriteOperateLog(msg);
     }
     catch (Exception ex)
     {
         LogerHelper.WriteErrorLog(ex);
     }
 }
Пример #8
0
        /// <summary>
        /// 停止某个服务
        /// </summary>
        /// <param name="serviceName"></param>
        private static void StopService()
        {
            try
            {
                var serviceName = ConfigurationManager.AppSettings["ServiceName"];
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName == serviceName)
                    {
                        service.Stop();
                        service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));//等待服务停止
                    }
                }

                string msg = string.Format("{1}:服务{0}停止完成!", serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                LogerHelper.WriteOperateLog(msg);
            }
            catch (Exception ex)
            {
                LogerHelper.WriteErrorLog(ex);
            }
        }