我開發在我所到Word(.DOC)將文件轉換爲文本文件中的應用,這裏是代碼示例:Word.Application.Quit()功能無法正常工作
//Creating the instance of Word Application
Word.Application newApp = new Word.Application();
// specifying the Source & Target file names
object Source = "F:\\wordDoc\\wordDoc\\bin\\Debug\\word.docx";
object Target = "F:\\wordDoc\\wordDoc\\bin\\Debug\\temp.txt";
object readOnly = true;
// Use for the parameter whose type are not known or
// say Missing
object Unknown = Type.Missing;
// Source document open here
// Additional Parameters are not known so that are
// set as a missing type;
newApp.Documents.Open(ref Source, ref Unknown,
ref readOnly, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown);
// Specifying the format in which you want the output file
object format = Word.WdSaveFormat.wdFormatDOSText;
object orgfrmat = Word.WdSaveFormat.wdFormatFilteredHTML;
//Changing the format of the document
newApp.ActiveDocument.SaveAs(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown);
// for closing the application
object saveChanges = Word.WdSaveOptions.wdSaveChanges;
newApp.Quit(ref saveChanges, ref Unknown, ref Unknown);
,但我的應用程序沒有正確關閉,當我嘗試使用此代碼
using (StreamReader sr = new StreamReader("F:\\wordDoc\\wordDoc\\bin\\Debug\\temp.txt"))
{
rtbText.Text = sr.ReadToEnd();
// Console.WriteLine(line);
}
它拋出該異常
該進程無法訪問讀取TEMP.TXT文件的內容文件'F:\ wordDoc \ wordDoc \ bin \ Debug \ temp.txt',因爲它正在被另一個進程使用。
誰能告訴我如何解決它?
那麼,爲什麼你認爲Word不關閉? 2個代碼片段之間的關係(時間)是什麼? – 2014-09-28 10:31:55
,因爲temp.txt已經在使用,兩個代碼都在同一個按鈕點擊事件 – 2014-09-28 10:38:46