【问题标题】:Get Running Instances of Word File获取 Word 文件的运行实例
【发布时间】:2017-11-02 08:08:45
【问题描述】:

我正在尝试读取 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

【问题讨论】:

  • 看来你的问题主要是代码,请添加一些cmets。
  • Path.GetFullPath() 不能只是神奇地查找您传递给它的任何路径。它所做的只是确保您没有相对路径,即如果您的应用程序的工作目录是 C:\Program Files (x86)\yourAppHello.txt 将变为 C:\Program Files (x86)\yourApp\Hello.txt
  • 那我怎么才能跳出默认路径
  • 你上面给出的链接是获​​取可执行文件的路径,但我想要 MainWindowsTitle 的路径

标签: .net vb.net process


【解决方案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

【讨论】:

    【解决方案2】:

    当你对每个循环使用 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
    

    【讨论】:

    • 它得到这个路径 C:\Program Files (x86)\Microsoft Office\Office16 作为我在这个路径中的文件 "C:\Users\The Networks\Desktop\About New.docx"跨度>
    • 它为您提供了流程。使用 a.MainWindowTitle 打开文件名,但这不会为您提供该文件的路径。
    • 亲爱的您提供的解决方案是只访问 MyDocuments 文件夹,而我想访问整个 PC。
    • 将 My.Computer.FileSystem.SpecialDirectories.MyDocuments 替换为 "c:\"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多