using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace rycUpdate
{
    class clsPram
    {
        // 结束一个进程
        [DllImport("kernel32", EntryPoint="TerminateProcess", SetLastError = false, 
        CharSet = CharSet.Auto, ExactSpelling = false, 
        CallingConvention = CallingConvention.StdCall)]
        private static extern int TerminateProcess(int hProcess,int uExitCode);

        public static int myHandle = 0;
        public static string UpdateZipPath = "";//升级文件压缩包所在位置
        public static string UnZipPath = "";//解压位置
        /// <summary>
        /// 原文件名
        /// </summary>
        public static string UpdateFileName = "rycUpdate.exe";//升级文件的文件名
        /// <summary>
        /// 更新的文件名
        /// </summary>
        public static string newUpdateFileName = "rycUpdate2.exe";//升级文件的文件名
        public static string MainName = "MyTaobao.exe";//主程序文件名
        /// <summary>
        /// 结束指定进程
        /// </summary>
        /// <param name="ProcName"></param>
        public static void KillProcess(string ProcName)
        {
            try
            {
                string ProcName_noext = System.IO.Path.GetFileNameWithoutExtension(ProcName);
                System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName(ProcName_noext);
                foreach (System.Diagnostics.Process p in process)
                {
                    try
                    {
                        if (myHandle != p.Handle.ToInt32())
                        {
                            if (p.MainModule.ModuleName.ToLower() == ProcName.ToLower())
                            {
                                p.Kill();
                                TerminateProcess(p.Handle.ToInt32(), 0);
                            }
                        }
                    }
                    catch {}
                }
            }
            catch {}
        }
        public static void KillMultiProcess(string ProcNames)
        {
            try
            {
                string[] procItem = ProcNames.Replace("\r", "").Split("\n".ToCharArray());
                for (int i = 0; i < procItem.Length; i++)
                {
                    KillProcess(procItem[i]);
                }
            }
            catch { }
        }
        /// <summary>
        /// 判断是否存在指定进程
        /// </summary>
        /// <param name="ProcName"></param>
        /// <returns></returns>
        public static bool HaveProcess(string ProcName)
        {
            string ProcName_noext = System.IO.Path.GetFileNameWithoutExtension(ProcName);
            System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName(ProcName_noext);
            if (process.Length > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
