获取当前 运行 c# 的 docx 的路径和名称

get the path and name of the docx that is currently running c#

我正在为 word 编写一个加载项,在下面的代码中,我将文件中的所有单词放入字典中。

`
   Dictionary<string, string> motRap = new Dictionary<string, string>();
   Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
   Document document = application.Documents.Open("monfichiertxt.docx");

        // Loop through all words in the document.
        int count = document.Words.Count;
        for (int i = 1; i <= count; i++)
        {
            // Write the word.
            string text = document.Words[i].Text;
            //motRapport.Add(text);
            motRap.Add(text, "blabla");
        }
        // Close word.
        application.Quit();

我想要的是当前 运行 的 docx 文件名,而不是写 "monfichiertxt.docx"。

有人可以帮帮我吗 谢谢。

这个问题以前有人处理过,没试过,1google,2行代码,简单回答。虽然作为一个插件,我没想到你需要这样做来获得你已经参与的单词实例..

    Microsoft.Office.Interop.Word.Application word = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
    label1.Text = word.ActiveDocument.Name;

您可以像这样使用 Name property or the FullName 属性

string name = document.Name;
string fullName = document.FullName;

Name 会给你 "MyDocument.docx" 而 FullName 会给你 "...\MyFolder\MyDocument.docx"