Saturday, June 11, 2011

get opened folder location in explorer using vb net

get opened folder location in explorer using vb net
I make it using VS 2010, in 2008 I think same. lets make new Visual Studio Project, Windows Form Application. and import this reference "" look this:

Vb net Add references Microsoft Shell Controls and Automation
Add references "Microsoft Shell Controls and Automation"
press "OK", and the begining of the code (before class) import this:

Imports Shell32

 and then copy this code to your project:

Public Class Form1
  Dim Lb As New ListBox
  Dim WithEvents RoutineTimer As New Timer
  Private Sub Form1_Load() Handles MyBase.Load
    Lb.Parent = Me
    Lb.Dock = DockStyle.Fill
    Me.Text = "Opened Folder by user (explorer)"
    RoutineTimer.Interval = 1000
  End Sub
  Sub GetOpenedFolder()
    Dim MShell As New Shell
    Dim SFV As ShellFolderView
    On Error Resume Next
    For Each o In MShell.Windows
      If TypeName(o.document) <> "HTMLDocument" Then
        SFV = o.document
        If SFV.Folder.Items.Count > 0 Then
          Lb.Items.Add(TrimPath(CType(SFV.Folder.Items(0), _
        End If
      End If
  End Sub

  Sub Timer_Job() Handles RoutineTimer.Tick
  End Sub

  Function TrimPath(ByRef s As String) As String
    Return s.Remove(InStrRev(s, "\"))
  End Function
End Class

The code will generate a timer and listbox in the form, so you don't have to adding control in the form. you can modify the code to running like a "On Access Scanner Program", have a nice program :-)

To test the program, Open two or more explorer, and then start the program.. and see what you get.

if you need to download the project, CLICK HERE TO DOWNLOAD


Anonymous said...

hello, Is there a way to do this but in batch?
Could someone tell me how it can be done?

Post a Comment