1

I made some research, but I can't find something really "interesting". I tried my best to find any kind of documentation or questions that are closest to my case as following:

How to find main window title name of application

how to get the window title of a process

How to get the Title Bar Text by its Process Id

getting the name of a process

How do I get list of Process Names running

Check to see if process is running

How To Get Process Owner ID

How to get the title/name of the last active window?

Get Process ID from Window Title

and also Process.GetProcessesByName Method

The code I am using to open the process window

Private Async Function ParentMethod() As Task
       Dim filePath As String = Await Task.Run(
       Function()

           Return Directory.EnumerateFiles(My.Settings.Cartellasalvataggio, titolo & ".mp3",
                         SearchOption.AllDirectories).FirstOrDefault()
       End Function)
       If Not String.IsNullOrEmpty(filePath) Then
           LinkLabel1.Text = "File exist already"
           LinkLabel1.Visible = True
           PictureBox7.Visible = True
       Else
           MsgBox("it doesn't exist")

       End If

   End Function

and the helper class

Imports System.IO
Imports System.Runtime.InteropServices

Public Class NativeMethods
   <DllImport("shell32.dll", SetLastError:=True)>
   Private Shared Function SHOpenFolderAndSelectItems(
           pidlFolder As IntPtr, cidl As UInteger,
           <[In], MarshalAs(UnmanagedType.LPArray)> apidl As IntPtr(),
           dwFlags As UInteger) As Integer
   End Function

   <DllImport("shell32.dll", SetLastError:=True)>
   Private Shared Sub SHParseDisplayName(
           <MarshalAs(UnmanagedType.LPWStr)> name As String,
           bindingContext As IntPtr, <Out> ByRef pidl As IntPtr,
           sfgaoIn As UInteger, <Out> ByRef psfgaoOut As UInteger)
   End Sub

   Public Shared Sub OpenFolderAndSelectFile(filePath As String)
       Dim dirPath As String = Path.GetDirectoryName(filePath)
       Dim fileName As String = Path.GetFileName(filePath)
       OpenFolderAndSelectFile(dirPath, fileName)
   End Sub

   Public Shared Sub OpenFolderAndSelectFile(dirPath As String, fileName As String)
       Dim nativeFolder As IntPtr
       Dim psfgaoOut As UInteger
       SHParseDisplayName(dirPath, IntPtr.Zero, nativeFolder, 0, psfgaoOut)

       If nativeFolder = IntPtr.Zero Then
           ' Log error, can't find folder
           Return
       End If

       Dim nativeFile As IntPtr
       SHParseDisplayName(Path.Combine(dirPath, fileName),
                          IntPtr.Zero, nativeFile, 0, psfgaoOut)

       Dim fileArray As IntPtr()
       If nativeFile = IntPtr.Zero Then
           ' Open the folder without the file selected if we can't find the file
           fileArray = New IntPtr(-1) {}
       Else
           fileArray = New IntPtr() {nativeFile}
       End If

       SHOpenFolderAndSelectItems(nativeFolder, CUInt(fileArray.Length), fileArray, 0)

       Marshal.FreeCoTaskMem(nativeFolder)
       If nativeFile <> IntPtr.Zero Then
           Marshal.FreeCoTaskMem(nativeFile)
       End If
   End Sub
End Class

then calling it with

NativeMethods.OpenFolderAndSelectFile(filepath,filename & "extension"))

Since I am opening the process this way and NOT with Process class, almost all of them are not suitable to be considered for my case as many of them refer to notepad, while I think the explorer window title and ID changes for every file ( obviously), while "notepad" process, stay "notepad".

I also tried BringToFront, but this latter moves a control in front of other controls, but in this case Explorer is not a control, right?

The least I want to do is to Get a list of active windows & their process names as It will waste memory and time usage for no reason as I will need to "filter" process to find my process.

Hope we can find a solution to this, Thanks in advance. Mattia

Mattia
  • 258
  • 7
  • 25
  • This is a bad idea, to begin with. If your problem is that the Explorer window goes behind the TopMost form, simply minimizing the form (`Me.WindowState = FormWindowState.Minimized`) before calling `OpenFolderAndSelectfile()` should be enough. Side note: `filePath` in the code above already includes the file name and extension; you don't need the `, filename & "extension"` part. – 41686d6564 stands w. Palestine Oct 21 '20 at 21:49
  • 1
    I was thinking to set top most false before to call OpenFolderAndSelectFile() and then set it true when the user mouse is on top of the form again, but I don't like either this or that solutions.. I don't want to minimize the form before calling it and I don't to use my solution as the form will minimize as soon as the user interact with the Explorer window.. – Mattia Oct 21 '20 at 21:53
  • Then, create your own viewer to display the directory's files _on the form_ (or on a child form) instead of using Explorer. In my opinion, minimizing the form is a much simpler solution. The user knows how to restore it once they're done with the Explorer window. Anyway, if you really want to (embed Explorer or create your own), you'll find some resources in [this post](https://stackoverflow.com/q/542378/8967612) but it's really not worth it. Don't overcomplicate things or try to solve a problem that doesn't exist. – 41686d6564 stands w. Palestine Oct 21 '20 at 21:56
  • 1
    Often. the user can interact with explorer window with just 1 click.. to me it seems a waste of time for user re open the software even if it is from tray. I am already displaying the path of the file on the form, so It would be great to complete with this function.. Also, note I'm trying to use so as the user is choosing in software option either to set top most true or false.. If user choose top most false then this is not even a problem, but if the user choose top most true then I feel I am not respecting its choises – Mattia Oct 21 '20 at 22:02

1 Answers1

1

This is the solution to it using FindWindowW e SetWindowPos Api. It is showing Explorer folder on top of top most form.

   <DllImport("user32.dll", EntryPoint:="FindWindowW")>
   Public Shared Function FindWindowW(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpClassName As String, <MarshalAs(UnmanagedType.LPTStr)> ByVal lpWindowName As String) As IntPtr
   End Function
   <DllImport("user32.dll")>
   Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As Boolean
   End Function
Shared ReadOnly HWND_TOPMOST As IntPtr = New IntPtr(-1)
   Const SWP_NOSIZE As UInt32 = &H1
   Const SWP_NOMOVE As UInt32 = &H2
   Const SWP_SHOWWINDOW As UInt32 = &H40
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


       Dim inptr = FindWindowW("CabinetWClass", Nothing)
       SetWindowPos(inptr, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
   End Sub
Mattia
  • 258
  • 7
  • 25