您可以考虑使用此选项,而不是在引用中添加互操作性:
System.Diagnostics.Process.Start(@"C:\Test\NewDocument.docx");
using var _ = System.Diagnostics.Process.Start(@"C:\Test\NewDocument.docx");
. - Mike Lowery首先将 Microsoft.Office.Interop.Word
的 dll 添加到您的引用中,然后添加以下内容:
using Microsoft.Office.Interop.Word;
并使用以下代码:
Application ap = new Application();
Document document = ap.Documents.Open(@"C:\Test\NewDocument.docx");
ap.Visible = true;
使Word可见。否则,它会打开文档,但窗口是不可见的。 - Daniel BonettiThe type or namespace name 'Application' does not exist in the namespace
- user2924019这个 Application
不是 this.Application
,而是 Microsoft.Office.Interop.Word.Application
。
所以你可以使用以下代码:
using System;
using Microsoft.Office.Interop.Word;
class Program
{
static void Main()
{
// Open a doc file.
Application application = new Application();
Document document = application.Documents.Open("C:\\word.doc");
//Do whatever you want
// Close word.
application.Quit();
}
}
上面有一个很好的答案,即:
System.Diagnostics.Process.Start(@"C:\Test\NewDocument.docx");
这应该针对 .Net Core 2 及以上进行修改:
var p = new Process();
p.StartInfo = new ProcessStartInfo(filename)
{
UseShellExecute = true
};
p.Start();
Application.Documents.Open()
。 - user585968