获取Word文件的运行实例

问题描述 投票:1回答:2

我正在尝试读取word文件路径,但它在vb.net中返回错误的路径并且我正在使用Path.getfullpath

For Each a In p
    If Not pName.Equals("") And I <= p.Count Then
        Console.WriteLine(a)
        Console.WriteLine(p.Count)
        pName = p(I).MainWindowTitle.ToString
        File.WriteLine("Word Process Name : {0} is started on time {1}", pName, p(I).StartTime)
        fullPath = Path.GetFullPath(pName)
        File.WriteLine("Path Of the file is  : {0}", fullPath(0))
    End If
Next
.net vb.net process
2个回答
1
投票

你也可以Microsoft.Office.Interop.Word图书馆

Dim wordApp As Microsoft.Office.Interop.Word.Application
wordApp = Marshal.GetActiveObject("Word.Application")
FileTxt = My.Computer.FileSystem.OpenTextFileWriter("E:\txt.txt", True)
For Each f In wordApp.Documents
      pName = Path.GetFileName(f.FullName).ToString()
      pPath = f.Path.ToString()
      FileTxt.WriteLine("Word Process Name : {0}  ", pName)
      FileTxt.WriteLine("Path of File : {0} " , pPath)
Next

0
投票

当你为每个循环使用a时,避免使用p(I)而不是“a”。这将返回所有单词进程打开

 Dim p() As Process = System.Diagnostics.Process.GetProcessesByName("winword")
        Dim List As New List(Of String)
        Dim cList As New List(Of Int32)
        Dim I As Int32 = 0
        If p.Count > 0 Then
            For Each a In p
                Dim fullpath As String = ""
                Console.WriteLine(a)
                Console.WriteLine(p.Count)
                Console.WriteLine("Word Process Name : {0} is started on time {1}", a.MainWindowTitle, a.StartTime.ToString)
                fullpath = Path.GetDirectoryName(a.MainModule.FileName)
                Console.WriteLine("Path Of the file is  : {0}", fullpath)
                cList.Add(I)
                I += 1
                List.Add(a.MainWindowTitle)
            Next
        Else
            'Word not open
        End If

您可以使用类似的东西在系统中查找文件,但这可能需要一段时间。

For Each foundFile As String In My.Computer.FileSystem.GetFiles(
    My.Computer.FileSystem.SpecialDirectories.MyDocuments,
    Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, a.MainWindowTitle)

    msgbox(foundFile)
Next
© www.soinside.com 2019 - 2024. All rights reserved.