Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB.net: Getting the list of files open in an application

Posted on 2011-03-03
8
951 Views
Last Modified: 2012-05-11
How can I get, using VB.net,  a list of all files open in an application for example Excel or Autocad?
0
Comment
Question by:Saqib Husain, Syed
  • 3
  • 3
  • 2
8 Comments
 
LVL 12

Expert Comment

by:silverkorn
ID: 35029454
are you trying to get a list of files open over the network or files opened on the current workstation?
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 35029517
Current workstation
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35031706
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 35035309
I need code to do this as I am new to VB.net
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35040524
It turns out listing used files is not as simple as it seems.  It requires calls to low level Windows API's that unless the code is running as a driver will very likely hang.  More details about this issue on the links below:


The solution below relies on a small but powerfull Sysinternal utlity called Handle by Mark Russinovich.  This utility displays information about open handles for any process in the system.

The sample code below executes Handle and captures its output, which is then parsed to retrieve the list of open files.

Before trying the sample, download Handle from the link below, include handle.exe (400KB) to your VS project, and set it to 'Copy to Output Directory = Copy if newer'.


Imports System.IO

Module Module1

    Sub Main()
        Dim pr As Process

        For Each pr In Process.GetProcessesByName("excel")
            Dim file As String

            For Each file In GetFilesOpenedByProcess(pr)
                Console.WriteLine(file)
            Next
        Next

    End Sub

    Private Function GetFilesOpenedByProcess(ByVal theProcess As Process) As List(Of String)
        Dim cmd As New Process()
        Dim cmdInfo As ProcessStartInfo = New ProcessStartInfo("handle", "-p " & theProcess.Id.ToString())
        Dim files As List(Of String) = New List(Of String)

        cmdInfo.UseShellExecute = False
        cmdInfo.RedirectStandardOutput = True

        cmd.StartInfo = cmdInfo
        cmd.Start()

        Dim rdr As StreamReader = cmd.StandardOutput
        Dim output As String = rdr.ReadToEnd()

        cmd.WaitForExit()
        cmd.Close()

        Dim lines() As String = output.Split(vbCr)
        Dim line As String

        For Each line In lines
            If line.Length > 22 Then
                If line.Substring(8, 4) = "File" Then
                    files.Add(line.Substring(22))
                End If
            End If
        Next

        Return files
    End Function

End Module

Open in new window


This code works on my Windows XP machine, but I have not tried it on other OS versions yet.

I hope this helps.
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 35044387
I really do not have any intention to go into APIs and handles.

I had been accessing autocad documents from excel and excel documents from autocad using methods like

set acd=getobject(,"Autocad.application")
for each dwg in acd.documents
.
.
.
next dwg

and I was hoping to do the same in vb.net with appropriate syntax. (The above did not work)

Saqib
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35053315
Try the following for Excel.  I'm not familiar with Autocad, but it should be similar.

Dim excel As Object
Dim wb As Object

Try
	excel = System.Runtime.InteropServices.Marshal.GetActiveObject ( "Excel.Application" )

	For Each wb In excel.Workbooks
		Console.WriteLine ( wb.FullName )
	Next

	excel = Nothing
Catch
	Console.WriteLine("Excel is not open.")
End Try

Open in new window

0
 
LVL 12

Expert Comment

by:silverkorn
ID: 35056204
For AutoCAD, here is some sample code from the AutoCAD .NET Developer's Guide, found here:
http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS73099cc142f48755-5c83e7b1120018de8c0-2202.htm,topicNumber=d0e3474

 
Imports System
Imports System.Runtime.InteropServices
 
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
 
<CommandMethod("ConnectToAcad")> _
Public Sub ConnectToAcad()
  Dim acAppComObj As AcadApplication
  Dim strProgId As String = "AutoCAD.Application.18"
 
  On Error Resume Next
 
  '' Get a running instance of AutoCAD
  acAppComObj = GetObject(, strProgId)
 
  '' An error occurs if no instance is running
  If Err.Number > 0 Then
      Err.Clear()
 
      '' Create a new instance of AutoCAD
      acAppComObj = CreateObject("AutoCAD.Application.18")
 
      '' Check to see if an instance of AutoCAD was created
      If Err.Number > 0 Then
         Err.Clear()
 
         '' If an instance of AutoCAD is not created then message and exit
         MsgBox("Instance of 'AutoCAD.Application' could not be created.")
 
         Exit Sub
      End If
  End If
 
  '' Display the application and return the name and version
  acAppComObj.Visible = True
  MsgBox("Now running " & acAppComObj.Name & " version " & acAppComObj.Version)
 
  '' Get the active document
  Dim acDocComObj As AcadDocument
  acDocComObj = acAppComObj.ActiveDocument
 
  '' Optionally, load your assembly and start your command or if your assembly
  '' is demandloaded, simply start the command of your in-process assembly.
  acDocComObj.SendCommand("(command " & Chr(34) & "NETLOAD" & Chr(34) & " " & _
                          Chr(34) & "c:/myapps/mycommands.dll" & Chr(34) & ") ")
 
  acDocComObj.SendCommand("MyCommand ")
End Sub

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question