微信号:
当前位置:首页 > 资讯文摘 > 新闻资讯

c#清除word进程和excel进程

2015/10/28 10:29:25

        #region 清除word进程
        /**//// <summary>
        /// 清除word进程
        /// </summary>
        public static void KillWordProcess()
        {
            System.Diagnostics.Process[] myPs;
            myPs = System.Diagnostics.Process.GetProcesses();
            foreach( System.Diagnostics.Process p in myPs )
            {
                if( p.Id != 0 )
                {
                    string myS = "WINWORD.EXE" + p.ProcessName + "  ID:" + p.Id.ToString();
                    try
                    {
                        if( p.Modules != null )
                            if( p.Modules.Count > 0 )
                            {
                                System.Diagnostics.ProcessModule pm = p.Modules[0];
                                myS += "/n Modules[0].FileName:" + pm.FileName;
                                myS += "/n Modules[0].ModuleName:" + pm.ModuleName;
                                myS += "/n Modules[0].FileVersionInfo:/n" + pm.FileVersionInfo.ToString();
                                if( pm.ModuleName.ToLower() == "winword.exe" )
                                    p.Kill();
                            }
                    }
                    catch
                    { }
                    finally
                    {

                    }
                }
            }
        }
        #endregion

        #region 清除excel进程
        /**//// <summary>
        /// 清除excel进程
        /// </summary>
        public static void KillExcelProcess()
        {
            System.Diagnostics.Process[] myPs;
            myPs = System.Diagnostics.Process.GetProcesses();
            foreach( System.Diagnostics.Process p in myPs )
            {
                if( p.Id != 0 )
                {
                    string myS = "excel.EXE" + p.ProcessName + "  ID:" + p.Id.ToString();
                    try
                    {
                        if( p.Modules != null )
                            if( p.Modules.Count > 0 )
                            {
                                System.Diagnostics.ProcessModule pm = p.Modules[0];
                                myS += "/n Modules[0].FileName:" + pm.FileName;
                                myS += "/n Modules[0].ModuleName:" + pm.ModuleName;
                                myS += "/n Modules[0].FileVersionInfo:/n" + pm.FileVersionInfo.ToString();
                                if( pm.ModuleName.ToLower() == "excel.exe" )
                                    p.Kill();
                            }
                    }
                    catch
                    { }
                    finally
                    {

                    }
                }
            }
        }
        #endregion

相关新闻: