Exemplo n.º 1
0
        /// <summary>
        /// 递归删除文件夹下所有文件
        /// </summary>
        /// <param name="strDir"></param>
        public static void Del_Files4Dir(string strDir)
        {
            if (strDir.IsNullOrWhiteSpace())
            {
                return;
            }

            if (!Directory.Exists(strDir))
            {
                return;
            }

            var full_Names = GetAll_Files4Dir(strDir);

            for (int i = 0; i < full_Names.Length; i++)
            {
                try
                {
                    var m_File_Status = FileExt.Check_Status(full_Names[i]);
                    if (m_File_Status == File_Status.Unlocked)
                    {
                        File.Delete(full_Names[i]);
                    }
                }
                catch (Exception ex)
                {
                    NLogHelper.Error(ex);
                }
            }
        }
Exemplo n.º 2
0
        public static void MoveToSpecialFilePath(string sourceHttpFilePath, string targetFilePath)
        {
            if (string.IsNullOrEmpty(targetFilePath))
            {
                NLogHelper.Error(string.Format("目标文件路径为空;源文件路径为:{0}。"));
                return;
            }

            if (!CheckDirectory(GetPathDirectory(targetFilePath, '\\')))
            {
                return;
            }

            try
            {
                HttpWebRequest  request        = (HttpWebRequest)WebRequest.Create(sourceHttpFilePath);
                HttpWebResponse response       = request.GetResponse() as HttpWebResponse;
                Stream          responseStream = response.GetResponseStream();

                using (System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding(936)))
                {
                    File.WriteAllText(targetFilePath, reader.ReadToEnd().Replace("\r", "\r\n").Replace("\r\n\n", "\r\n"), Encoding.GetEncoding(936));
                }

                responseStream.Close();
            }
            catch (Exception ex)
            {
                NLogHelper.Error(ex);
                return;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 将服务设置为 自动启动,并启动未启动的服务
        /// </summary>
        /// <param name="service"></param>
        public static void StartService(ServiceController service)
        {
            if (service == null)
            {
                return;
            }

            try
            {
                if (WinSrvHelper.GetStartupType(service) != "AUTOMATIC")
                {
                    ChangeStartMode(service, ServiceStartMode.Automatic);  // 将服务设置为 自动启动
                }

                if (service.Status == ServiceControllerStatus.Running) // 已启动的服务不能再次启动
                {
                    return;
                }

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running);
            }
            catch (Exception ex)
            {
                NLogHelper.Error(ex);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 确保带配置文件的exe程序可以顺利执行,路径不能带空格
        /// </summary>
        /// <param name="proc_fullPath">F:\Program Files (x86)\Notepad++\notepad++.exe</param>
        public static void Run_Proc(string proc_fullPath)
        {
            using (Process proc = new Process())
            {
                proc.StartInfo.FileName               = "cmd.exe"; //打开cmd程序
                proc.StartInfo.UseShellExecute        = false;     //不使用shell启动程序
                proc.StartInfo.RedirectStandardInput  = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError  = true;
                proc.StartInfo.CreateNoWindow         = true; // true表示不显示黑框,false表示显示dos界面

                try
                {
                    proc.Start();                                //启动

                    proc.StandardInput.WriteLine(proc_fullPath); //执行程序具体位置

                    proc.WaitForExit();
                }
                catch (Exception ex)
                {
                    NLogHelper.Error(ex);
                }
            }
        }
Exemplo n.º 5
0
        public static void Safe_Del(string strFile)
        {
            if (!File.Exists(strFile))
            {
                return;
            }

            try
            {
                File.Delete(strFile);
            }
            catch (Exception ex)
            {
                NLogHelper.Error(ex);
            }
        }
Exemplo n.º 6
0
        public static void StopService(string serviceName)
        {
            ServiceController controller = GetService(serviceName);

            if (controller == null)
            {
                return;
            }

            try
            {
                if (controller.Status == ServiceControllerStatus.Running)
                {
                    controller.Stop();
                    controller.WaitForStatus(ServiceControllerStatus.Stopped);
                }
            }
            catch (Exception ex)
            {
                NLogHelper.Error(ex);
            }
        }
Exemplo n.º 7
0
        public static void End_Proc(List <string> proc_Names)
        {
            using (Process proc = new Process())
            {
                proc.StartInfo.FileName               = "cmd.exe"; //打开cmd程序
                proc.StartInfo.UseShellExecute        = false;     //不使用shell启动程序
                proc.StartInfo.RedirectStandardInput  = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError  = true;
                proc.StartInfo.CreateNoWindow         = true; // true表示不显示黑框,false表示显示dos界面
                proc.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;

                string m_proc_Name = "";
                try
                {
                    proc.Start();//启动

                    for (int i = 0; i < proc_Names.Count; i++)
                    {
                        if (proc_Names[i].EndsWith(".exe"))
                        {
                            m_proc_Name = proc_Names[i];
                        }
                        else
                        {
                            m_proc_Name = proc_Names[i] + ".exe";
                        }
                        proc.StandardInput.WriteLine(string.Format("taskkill /f /im {0}", m_proc_Name));// 关闭进程
                    }

                    proc.StandardInput.WriteLine("exit"); //退出
                    proc.Close();                         //关闭
                }
                catch (Exception ex)
                {
                    NLogHelper.Error(ex);
                }
            }
        }
Exemplo n.º 8
0
        public static void SafeChangeStartMode(string serviceName, ServiceStartMode mode)
        {
            ServiceController service = GetService(serviceName);

            if (service == null)
            {
                return;
            }

            string strMode = mode.ToString().ToUpper().TrimStart("SERVICESTARTMODE.");

            try
            {
                if (WinSrvHelper.GetStartupType(service) != strMode)
                {
                    ChangeStartMode(service, mode);  // 将服务设置为 自动启动
                }
            }
            catch (Exception ex)
            {
                NLogHelper.Error(ex);
            }
        }
Exemplo n.º 9
0
        public static bool DeleteFilesBySpecialPath(string path)
        {
            if (Directory.Exists(path) == false)
            {
                NLogHelper.Equals("文件夹{0}不存在", path);
                return(false);
            }
            DirectoryInfo dir = new DirectoryInfo(path);

            FileInfo[] files = dir.GetFiles();
            try
            {
                foreach (var item in files)
                {
                    File.Delete(item.FullName);
                }
                if (dir.GetDirectories().Length != 0)
                {
                    foreach (var item in dir.GetDirectories())
                    {
                        if (!item.ToString().Contains("$") && (!item.ToString().Contains("Boot")))
                        {
                            DeleteFilesBySpecialPath(dir.ToString() + "\\" + item.ToString());
                        }
                    }
                }
                //Directory.Delete(path);

                return(true);
            }
            catch
            {
                NLogHelper.Equals("{0}删除异常", path);
                return(false);
            }
        }