命名空間 :System.Diagnostics測試
之前在word的時候,常常碰到word進程產生一大堆,怕關錯了,把用戶本身打開的word也關閉,一直搞忽悠,今天上網花了10塊錢,下了個文件,給我了一點啓發,總於能夠實現只關閉程序中打開的,,,我本身測試了,是能夠實現的,各位也能夠copy去試試,不行的話,給我留言。。。ui
//如下的定義爲打開或保存word文件時,須要用到得參數orm
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;ip
string str = pro.MainWindowTitle; //發現程序中打開跟用戶本身打開的區別就在這個屬性
//用戶打開的str 是文件的名稱,程序中打開的就是空字符串
if (string.IsNullOrEmpty(str))
{
pro.Kill();
}
}
}
catch (Exception ex)
{
ex.ToString();
}
}
}
catch (Exception ex)
{
bl = false;
}文檔