Try the following code:
namespace ProcessHandle
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Get the process handle example.");
var process = new Process
{
StartInfo = new ProcessStartInfo
("explorer.exe", @"c:\")
};
process.Start();
Console.WriteLine("Process Id: " + process.Id);
Console.WriteLine("Process Name: " + process.ProcessName);
Console.WriteLine("Process Handle(IntPtr): " +
process.Handle.ToString());
Console.ReadLine();
}
}
}
use the above code, you can get the handle of the new opened window,
and then use win api set the position.
On Jun 5, 5:14 pm, Bert <[email protected]> wrote:
> Hi All,
> I want to open a Windows explorer folder in my C# program, then reset
> the opened explorer window's position with SetWindowsPos via PInvoke.
> I do it in following way:
> Process p = Process.Start("explorer.exe", “C:\\”);
> p.WaitForInputIdle();
> IntPtr hWnd = p.MainWindowHandle;
> SetWindowPos(hWnd,...)....(do it with PInvoke)
> The problem is that when codes executes to "IntPtr hWnd =
> p.MainWindowHandle;", the windows handle hWnd I got is IntPtr.Zero,
> and the process p has exited already .
> And I traced the process p's id, it seems that the id is not same with
> the id of the system process "Explorer.exe" when I see it from Windows
> "Task Manager", so the process created in my code is not same with the
> process "Explorer.exe" in system. It seems that I could not get the
> handle of the new opened explorer window in such way.
> Then how could I get the handle of the new opened explorer window?
> Maybe someone would say that you could use "FindWindow" API to get it,
> but there maybe exist many explorer windows with the same folder
> location, so in such way I could not judge which explorer window is
> the one opened by my program.
> Maybe others would say that you could use "GetForegroundWindow" API to
> get the Foreground Window, yes I could get it in most of the time, but
> sometimes the new opened explorer window does not appear foreground,
> so it's not the right way also.
> Anyone has other ideas?
>
> Thanks.
> Bert