none
How Open and Close windows explorer by vb.net

    Question

  • hello every one,

        I have use VB.Net 2005, I want to create an application , In this I want to open a windows explorer on a form load event and close this windows explorer when my application will close or on formclosing event .

     

    regards

    Navaratan

     

    Saturday, May 30, 2009 9:27 AM

Answers

  • Hello Mr. Dig-Boy
                            Thanks for replay.

    That  I am using this code and it’s working good. Without any problem.

                     

    Option Explicit On

    Option Strict On

     

    Imports System.Runtime.InteropServices

    Imports Microsoft.VisualBasic

    Imports System.Diagnostics

    Imports System.Management

    Imports System

     

    Imports Infragistics.Win

    Imports Infragistics.Win.UltraWinEditors

     

    Public Class Finlly

        Dim vlProcess As New ValueList

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Try

                Dim processClass1 As New ManagementClass("Win32_Process")

                Dim inParams1 As ManagementBaseObject = processClass1.GetMethodParameters("Create")

                inParams1("CommandLine") = "explorer.exe /e, /root, D:\Windows Explore"

                Dim outParams As ManagementBaseObject = processClass1.InvokeMethod("Create", inParams1, Nothing)

                vlProcess.ValueListItems.Add(outParams("processId").ToString, outParams("processId").ToString)

            Catch ex As Exception

                MessageBox.Show(ex.Message.ToString)

            End Try

        End Sub

     

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Try

                Dim query As SelectQuery = New SelectQuery("SELECT * FROM Win32_Process WHERE Name = 'EXPLORER.EXE'")

                Dim mgmtSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(query)

                For Each p As ManagementObject In mgmtSearcher.Get()

                    Dim s(1) As String

                    p.InvokeMethod("GetOwner", DirectCast(s, Object()))

                   

                    For pCount As Integer = 0 To vlProcess.ValueListItems.Count - 1

                        Dim ProId As String = vlProcess.ValueListItems(pCount).DataValue().ToString

                        If p("Name").ToString().ToUpper = "EXPLORER.EXE" AndAlso p("ProcessId").ToString().ToLower.Trim = ProId.ToLower.Trim Then

                            p.InvokeMethod("Terminate", Nothing)

                            Exit For

                        End If

                    Next

     

                Next

            Catch ex As Exception

                MessageBox.Show(ex.Message.ToString)

            End Try

        End Sub

    End Class

     

     

    In place of valuelist you can use array or arraylist for this. Valuelist is third party control.

    But we use  Imports System.Management namespace for this you add reference System.Management.

     

    This is the solution of my problem.

    I send you my complete requirement  in next message.

    Again

    Thank’s

    Regards,

    Navaratan


                             

     

    • Edited by Navaratan Tuesday, June 02, 2009 10:01 AM code doesnot show
    • Marked as answer by Navaratan Wednesday, June 03, 2009 9:32 AM
    Tuesday, June 02, 2009 9:58 AM
  • Hi Navaratan 

    When somebody has helped you to get the answer whatever it was, then mark those replies as helpfull and mark them as answer. Otherwise you have the chance that people will remember your nick and give no or only limited answers to you in future, simply because they spent their time then to others questions.


    Success
    Cor
    • Proposed as answer by Amrishgm Wednesday, July 01, 2009 9:48 AM
    • Marked as answer by Navaratan Thursday, July 02, 2009 1:17 PM
    Wednesday, July 01, 2009 7:14 AM

All replies

  • Here's the code to do it with some innocuous program like Notepad...


    Public Class Form1
    
        Private ExplorerProcess As Process = Nothing
    
        Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            If ExplorerProcess IsNot Nothing AndAlso ExplorerProcess.HasExited = False Then
                ExplorerProcess.Kill()
            End If
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ExplorerProcess = Process.Start("notepad.exe")
        End Sub
    End Class

    ...but to start Windows Explorer !!!  That sounds like a really BAD idea.  Do you really mean to open and close the core Windows application or do you mean something else instead, like perhaps a folder browser?

    • Proposed as answer by A.m.a.L Hashim Saturday, May 30, 2009 12:30 PM
    Saturday, May 30, 2009 12:19 PM
  • Hello Mr.Dig-Boy
             Thank's for replay
        this code is working for all nitepade.exe,word, and so on........
      but when i code 

      ExploereProcess=Process.Start("expolere.exe")

    then it give error message to me on form closing.

    Monday, June 01, 2009 4:36 AM
  • I think you want to open the explorer showing some directory. In that case, use something like this


      ExploereProcess=Process.Start("C:\")

    As it is a path, it would be opened by an explorer window. You could replace the C:\ with your path as required.



    If this post is useful, mark it as answer.
    Monday, June 01, 2009 11:28 AM
  • You spelled EXPLORER wrong.  That's why it didn't work.  This will work:

            ExploereProcess = Process.Start("explorer.exe")


    Doug
    Monday, June 01, 2009 11:46 AM
  • After some testing it just confirms what I thought...  Explorer.exe is a protected resource and cannot be accessed like this.  You can get the process ID from the Process.Start call but not the process itself.  Immediately after the Start call the variable shows up as Nothing.  The ID you get is also invalid because it immediately references a lost process.  Even though a folder explorer is shown from the STart call, if you close it the explorer.exe does not go away in TaskManager because the explorer environment encompasses much more than the browser itself.  If you kill explorer.exe in TM you'll lose the desktop and taskbar -- not what I call a user-friendly action.  But if you search for all processes by name (explorer.exe) in the FormClosing event you'll get nothing!  This suggests to me that it is a protected resource. 

    I would stay away from it.  Can you explain why you need to be running explorer?  Maybe we can help you find an alternative.
    Monday, June 01, 2009 8:57 PM
  • Hello Mr. Dig-Boy
                            Thanks for replay.

    That  I am using this code and it’s working good. Without any problem.

                     

    Option Explicit On

    Option Strict On

     

    Imports System.Runtime.InteropServices

    Imports Microsoft.VisualBasic

    Imports System.Diagnostics

    Imports System.Management

    Imports System

     

    Imports Infragistics.Win

    Imports Infragistics.Win.UltraWinEditors

     

    Public Class Finlly

        Dim vlProcess As New ValueList

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Try

                Dim processClass1 As New ManagementClass("Win32_Process")

                Dim inParams1 As ManagementBaseObject = processClass1.GetMethodParameters("Create")

                inParams1("CommandLine") = "explorer.exe /e, /root, D:\Windows Explore"

                Dim outParams As ManagementBaseObject = processClass1.InvokeMethod("Create", inParams1, Nothing)

                vlProcess.ValueListItems.Add(outParams("processId").ToString, outParams("processId").ToString)

            Catch ex As Exception

                MessageBox.Show(ex.Message.ToString)

            End Try

        End Sub

     

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Try

                Dim query As SelectQuery = New SelectQuery("SELECT * FROM Win32_Process WHERE Name = 'EXPLORER.EXE'")

                Dim mgmtSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(query)

                For Each p As ManagementObject In mgmtSearcher.Get()

                    Dim s(1) As String

                    p.InvokeMethod("GetOwner", DirectCast(s, Object()))

                   

                    For pCount As Integer = 0 To vlProcess.ValueListItems.Count - 1

                        Dim ProId As String = vlProcess.ValueListItems(pCount).DataValue().ToString

                        If p("Name").ToString().ToUpper = "EXPLORER.EXE" AndAlso p("ProcessId").ToString().ToLower.Trim = ProId.ToLower.Trim Then

                            p.InvokeMethod("Terminate", Nothing)

                            Exit For

                        End If

                    Next

     

                Next

            Catch ex As Exception

                MessageBox.Show(ex.Message.ToString)

            End Try

        End Sub

    End Class

     

     

    In place of valuelist you can use array or arraylist for this. Valuelist is third party control.

    But we use  Imports System.Management namespace for this you add reference System.Management.

     

    This is the solution of my problem.

    I send you my complete requirement  in next message.

    Again

    Thank’s

    Regards,

    Navaratan


                             

     

    • Edited by Navaratan Tuesday, June 02, 2009 10:01 AM code doesnot show
    • Marked as answer by Navaratan Wednesday, June 03, 2009 9:32 AM
    Tuesday, June 02, 2009 9:58 AM
  • If Dig-Boy's reply helped you solve your problem, you should mark HIS post as the Answer.



    Doug
    Thursday, June 04, 2009 12:59 AM
  • Hi there,

        As you suggested, I use ArrayList instead of ValueList and made other changes accordingly but it didnt work.

    My current project require me to open and close Window Explorer on buton click event and I am working it for around half of month but didnt get any solution. I desperately need any solution for this, Following are your code I had changed, if I did anything wrong please let me know...

    Dim vlProcess As New ArrayList

    Private Sub btnExStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExStart.Click
            Try

                Dim processClass1 As New ManagementClass("Win32_Process")
                Dim inParams1 As ManagementBaseObject = processClass1.GetMethodParameters("Create")
                inParams1("CommandLine") = "explorer.exe /e, /root, D:\Windows Explore"
                Dim outParams As ManagementBaseObject = processClass1.InvokeMethod("Create", inParams1, Nothing)
                vlProcess.Add(outParams("processId").ToString)

            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString)
            End Try
        End Sub

        Private Sub btnExClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExClose.Click
            Try

                Dim query As SelectQuery = New SelectQuery("SELECT * FROM Win32_Process WHERE Name = 'EXPLORER.EXE'")
                Dim mgmtSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(query)

                For Each p As ManagementObject In mgmtSearcher.Get()
                    Dim s(1) As String

                    p.InvokeMethod("GetOwner", DirectCast(s, Object()))

                    For pCount As Integer = 0 To vlProcess.Count - 1
                        Dim ProId As String = vlProcess.Item(pCount).ToString
                        If p("Name").ToString().ToUpper = "EXPLORER.EXE" AndAlso p("ProcessId").ToString().ToLower.Trim = ProId.ToLower.Trim Then
                            p.InvokeMethod("Terminate", Nothing)
                            Exit For
                        End If
                    Next
                Next

            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString)
            End Try
        End Sub
    • Proposed as answer by Amrishgm Wednesday, July 01, 2009 9:48 AM
    Wednesday, July 01, 2009 6:47 AM
  • Hi Navaratan 

    When somebody has helped you to get the answer whatever it was, then mark those replies as helpfull and mark them as answer. Otherwise you have the chance that people will remember your nick and give no or only limited answers to you in future, simply because they spent their time then to others questions.


    Success
    Cor
    • Proposed as answer by Amrishgm Wednesday, July 01, 2009 9:48 AM
    • Marked as answer by Navaratan Thursday, July 02, 2009 1:17 PM
    Wednesday, July 01, 2009 7:14 AM
  • hello amrish
          use this code for open and close windows eplorer on a paricure location.
    you just add refrence of  system.management  on .net tab. in this windowswxp is my form name.

    Imports
    System

    Imports System.Management

    Imports System.Diagnostics

    Imports System.Runtime.InteropServices

    Imports System.IO.Directory

     

     

    Public Class windowswxp

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            If IO.Directory.Exists("c:\Inetpub") Then

                OpenFolder("c:\Inetpub")

            End If

        End Sub

     

     

        Private HTProcess As New Hashtable

     

        Private Sub OpenFolder(ByVal Path As String)

            Try

                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\CabinetState", "FullPathAddress", 0)

                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", "SeparateProcess", 1)

     

                Dim processClass1 As New ManagementClass("Win32_Process")

                Dim inParams1 As ManagementBaseObject = processClass1.GetMethodParameters("Create")

                inParams1("CommandLine") = "explorer.exe /e, /root, " & Path

                Dim outParams As ManagementBaseObject = processClass1.InvokeMethod("Create", inParams1, Nothing)

                HTProcess.Add(outParams("processId").ToString, outParams("processId").ToString)

     

            Catch ex As Exception

                MessageBox.Show(ex.Message.ToString)

            End Try

        End Sub

     

        Private Sub CloseFolder()

            Try

                Dim query As SelectQuery = New SelectQuery("SELECT * FROM Win32_Process WHERE Name = 'EXPLORER.EXE'")

                Dim mgmtSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(query)

                For Each p As ManagementObject In mgmtSearcher.Get()

                    Dim s(1) As String

                    p.InvokeMethod("GetOwner", DirectCast(s, Object()))

                    Dim objEntry As DictionaryEntry

     

                    For Each objEntry In HTProcess

                        Dim ProId As String = objEntry.Value.ToString

                        If p("Name").ToString().ToUpper = "EXPLORER.EXE" AndAlso p("ProcessId").ToString().ToLower.Trim = ProId.ToLower.Trim Then

                            p.InvokeMethod("Terminate", Nothing)

                            Exit For

                        End If

                    Next

                Next

     

            Catch ex As Exception

                MessageBox.Show(ex.Message.ToString)

            End Try

        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            CloseFolder()

        End Sub

     

        Private Sub windowswxp_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

            CloseFolder()

        End Sub

     

      

    End Class

    • Proposed as answer by Amrishgm Monday, July 06, 2009 9:03 AM
    Friday, July 03, 2009 5:43 AM
  • Hello Navaratan ,

            Lots n lots of thanks!!!!!

            Your solution works perfectly, You cant beleive but I was finding solution for Closing Windows by VB.NET for around the month but didnt get any. I only make little change, instead of using
    "c:\Inetpub" , I used "c:\" and still it works fine. I found lots of people were searching for the same problem. now your solution surely help them. I really appreciate your help. Thanks again.
    Monday, July 06, 2009 9:12 AM
  • Didn't work for me
    the mighty man
    Thursday, April 15, 2010 4:25 AM
  • Hello mighty7

     can you give some more detail, what are you want to do.

    Friday, April 16, 2010 1:06 PM
  • Hi

    I have been using this solution for sometime now in my application to open up a windows explorer window and it works perfectly... in windows xp.

    But someone installed my app on Windows 7 and now if he opens folder using my app it still opens but it doesnt work properly. For example:

    If he uses context menu to create new folder it does not appear after it has been created. He has to press F5 for it to show up.

     

    Can you or someone else here is aware of that and know why this happens?

     

    Here is the code I use in order to open folder:

          My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\CabinetState", "FullPathAddress", 0)
          My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", "SeparateProcess", 1)
    
          Dim processClass1 As New ManagementClass("Win32_Process")
          Dim inParams1 As ManagementBaseObject = processClass1.GetMethodParameters("Create")
          inParams1("CommandLine") = "explorer.exe /e, /root, " & Path
          Dim outParams As ManagementBaseObject = processClass1.InvokeMethod("Create", inParams1, Nothing)
          HTProcess.Add(outParams("processId").ToString, outParams("processId").ToString)
    

     

    P.S. I'm really stuck with this and have no clue as to what causes this.

    Thanks in advance

     


    Cheers!
    Friday, February 04, 2011 10:16 AM