获取Word文件的运行实例

时间:2017-11-02 08:08:45

标签: .net vb.net process

我试图读取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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

aaa=000 bbb=111
aaa=555 bbb=666
aaa=444 bbb=555

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

 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