命名空间 :System.Diagnostics

以前在word的时候,经常碰到word进程产生一大堆,怕关错了,把用户自己打开的word也关闭,一直搞忽悠,今天上网花了10块钱,下了个文件,给我了一点启发,总于可以实现只关闭程序中打开的,,,我自己测试了,是可以实现的,各位也可以copy去试试,不行的话,给我留言。。。

//以下的定义为打开或保存word文件时,需要用到得参数

object confirmConversions = Type.Missing;
                object readOnly = Type.Missing;
                object addToRecentFiles = Type.Missing;
                object passwordDocument = Type.Missing;
                object passwordTemplate = Type.Missing;
                object revert = Type.Missing;
                object writePasswordDocument = Type.Missing;
                object writePasswordTemplate = Type.Missing;
                object format = Type.Missing;
                object encoding = Type.Missing;
                object visible = Type.Missing;
                object openConflictDocument = Type.Missing;
                object openAndRepair = Type.Missing;
                object documentDirection = Type.Missing;
                object noEncodingDialog = Type.Missing;
                object missing = Type.Missing;
                object filename = Filename;
                object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; //解决normal.dot问题

Microsoft.Office.Interop.Word._Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word._Document wordDoc = null;
                try
                {
                     
                    //打开Word文档对象  
                    wordDoc = wordApp.Documents.Open(ref filename, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDocument, ref passwordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding, ref visible, ref openConflictDocument, ref openAndRepair, ref documentDirection, ref noEncodingDialog);
                    wordDoc.AcceptAllRevisions(); //接收word中所有的修订
                    wordDoc.Save();//保存
                    wordDoc.Close(ref missing, ref missing, ref missing);
                    wordApp.Application.Quit(ref saveOption, ref missing, ref missing);
                }
                catch { }
                finally
                {

//杀死打开的word进程
                    Process myProcess = new Process();
                    Process[] wordProcess = Process.GetProcessesByName("winword");
                    try
                    {
                        foreach (Process pro in wordProcess) //这里是找到那些没有界面的Word进程
                        {
                           IntPtr ip= pro.MainWindowHandle;

string str = pro.MainWindowTitle; //发现程序中打开跟用户自己打开的区别就在这个属性
                            //用户打开的str 是文件的名称,程序中打开的就是空字符串
                            if (string.IsNullOrEmpty(str))
                            {
                                pro.Kill();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }

}
            }
            catch (Exception ex)
            {
                bl = false;
            }

[转]关闭word进程的更多相关文章

  1. C#关闭word进程

    C#关闭word进程 foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName(&q ...

  2. [转]C# 关闭嵌在程序中的word进程而不关闭用户通过word手动打开的word进程

    命名空间 :System.Diagnostics 以前在word的时候,经常碰到word进程产生一大堆,怕关错了,把用户自己打开的word也关闭,一直搞忽悠,今天上网花了10块钱,下了个文件,给我了一 ...

  3. C# 调用word进程操作文档关闭进程

    C# 调用word进程操作文档关闭进程 作者:Jesai 时间:2018-02-12 20:36:23 前言: office办公软件作为现在主流的一款办公软件,在我们的日常生活和日常工作里面几乎每天都 ...

  4. 彻底关闭Excle进程的几个方法

    之前研究过的问题,最近有朋友问,这里再总结下做一个笔记. 我们在应用程序里面通过创建Excle应用对象打开Excle的情况下,如果不注意几个问题,可能无法彻底关闭Excle进程,来考察下面的几种情况: ...

  5. 一张图搞定OAuth2.0 在Office应用中打开WPF窗体并且让子窗体显示在Office应用上 彻底关闭Excle进程的几个方法 (七)Net Core项目使用Controller之二

    一张图搞定OAuth2.0   目录 1.引言 2.OAuth2.0是什么 3.OAuth2.0怎么写 回到顶部 1.引言 本篇文章是介绍OAuth2.0中最经典最常用的一种授权模式:授权码模式 非常 ...

  6. C# 如何强制关闭WINWORD进程

    private void KillProcess(string processName) //调用方法,传参{try{ Process[] thisproc = Process.GetProcesse ...

  7. C#操作Word (2)-- 打开&关闭Word文档

    本文正式开始在VS2010中使用C#语言操作Word2007. 不是十分了解Word对象模型的朋友,请参考上一篇文章,或者下载:C#操作Word2007.pdf. ------------------ ...

  8. wxPython 对话框关闭后进程无法退出的原因

    wxPython中不要用对话框作为主程序wx.Dialog,这会导致程序关闭后进程无法退出.一种可行的做法是用wx.Frame代替

  9. windows和linux下关闭Tomcat进程

    windows和linux下解决Tomcat进程 windows下启动Tomcat报错,8080端口号被占用,报错信息如下 两种解决方法,一种是关闭了这个端口号,另外一种是修改Tomcat下的serv ...

随机推荐

  1. Systemd 基础(转)

    Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置. 原文链接:http://www.ruanyifeng.com/blog/2016/03/systemd-tu ...

  2. lenovo thinkpad L460 安装Win7阐述

    lenovo thinkpad L460 系统自带Win10切GPT分区,如果想安装Win7 01.需要改bios 02.清空硬盘全盘数据,调整分区类型MBR分区引导(需要全盘格式化,调整引导类型) ...

  3. 微软 Visual Studio 2017 中文正式版下载 – 免费社区版/专业版/企业版

    作为“宇宙最强”的集成开发环境 IDE,微软的 Visual Studio 不仅破天荒发布了 macOS 版本,如今终于也推出了其 Windows 的最新版本—— VS 2017 正式版了.这对开发者 ...

  4. 用curl去探测接口是否正常返回结果,若没有正常返回则触发报警

    现有一需求去curl 在香港的一个接口, 返回值有时正常有时报错 connection reset by peer . 思路: 若 执行成功 $?返回 0  , 不成功则返回其他数字 #!/bin/b ...

  5. Swift代理造成内存泄漏的解决办法

    在swift中,使用代理 ,可能很多人会这样实现: .首先定义一份协议. protocol ToolProrocol{ //代理方法 func didRecieveResults(result:Int ...

  6. 【LeetCode】27. Remove Element (2 solutions)

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  7. Socket详解-Linux Socket编程(不限Linux)

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  8. AME_IExpense费用报表通过AME审批简单例子(案例)

    2014-05-30 Created By BaoXinjian

  9. MAR 27 解决华为手机访问Google Play:从服务器检索信息时出错。[DF-DFERH-01]

    虽然路由器已经设置了梯子,但是用华为手机访问Google Play时,还是提示:从服务器检索信息时出错.[DF-DFERH-01].   虽然在手机上把梯子设置成全局模式,连接Google Play后 ...

  10. 小半斤拔凉 支付Java 相关参考

    http://git.oschina.net/littleCrazy/dianshangpingtai-zhifu http://git.oschina.net/52itstyle/springMvc ...