Link to home
Start Free TrialLog in
Avatar of systan
systanFlag for Philippines

asked on

get the path of an opened window before paste

Get the path of an opened window before paste;

Sample,
When I paste files to desktop, it message > The path before paste
is: C:\Documents and Settings\Administrator\Desktop\

When I paste files to my documents folder, it message > The path before paste
is: C:\Documents and Settings\Administrator\My Documents\
Avatar of systan
systan
Flag of Philippines image

ASKER

Ok; help me with this;
This gets the path, BUT gets the last opened windows, if you switch from another windows to another?, the last windows opened still detected.


private string s;
private void button1_Click(object sender, EventArgs e)
{

//Shell32 is located in Interop.Shell32 assembly
 Shell32.ShellClass shell = new ShellClass();

//Shellwindows is located in SHDocVw.dll under C:\windows
ShellWindows shellWindows = (ShellWindows)shell.Windows();

//Don't know how to apply this to shellWindow
////////IntPtr activeHwnd = GetForegroundWindow();

foreach (ShellBrowserWindow shellWindow in shellWindows)
{
//this is the path        
s= shellWindow.LocationURL;


//This is the object that holds Explorer window information.
ShellFolderView view = (ShellFolderView)shellWindow.Document;

//Add error handling and null checks here
foreach (FolderItem item in view.SelectedItems())
{
    MessageBox.Show(item.Path.ToString());
}

}


label1.Text = s;
}
ShellBrowserWindow has HWND property. You could compare handle from GetForegroundWindow with it.
Avatar of systan

ASKER

Don't know how to apply, please help, a code snippet would be great.
Avatar of systan

ASKER

GetForegroundWindow Can't handle it, even GetLastActivePopup.
There must be a way to get the current or last folder opened.
Avatar of systan

ASKER

//Shell32 is located in Interop.Shell32 assembly
Shell32.ShellClass shell = new ShellClass();

//Shellwindows is located in SHDocVw.dll under C:\windows
ShellWindows shellWindows = (ShellWindows)shell.Windows();

foreach (ShellBrowserWindow shellWindow in shellWindows)
{
//This is the object that holds Explorer window information.
ShellFolderView view = (ShellFolderView)shellWindow.Document;
//Get ALL currently active windows path
MessageBox.Show(view.FocusedItem.Path);
}


The problem is, is ALL, it list all,  BUT I want to get ONLY the last active window.

Please, someone, help, it's very impossible that a longtime experts here did not encounter this.
I already provided the code snippet, just a little line to solve the problem.
ASKER CERTIFIED SOLUTION
Avatar of Rimvis
Rimvis
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of systan

ASKER

Error      1      The name 'WinEventProc' does not exist in the current context ?
Avatar of systan

ASKER

Ok, I've made some research, here it is;
http://social.msdn.microsoft.com/Forums/en/vcmfcatl/thread/0dbbc02e-cfd1-4183-a252-c1e9d6166fd8
http://social.msdn.microsoft.com/Forums/en/clr/thread/c04e343f-f2e7-469a-8a54-48ca84f78c28
http://ctp.social.msdn.microsoft.com/Forums/en-US/winforms/thread/30ae2081-17ed-497a-942e-efb75bca6295

The problem is?, it only gets the title of the window by getText,  i'LL try to see if there is a fix, please help also.

Please note, that I am getting the LAST/current path of last/current explored window.


Thanks
Avatar of systan

ASKER

I think I got it,
thank you
Looks like I forgot to paste code for the event handler :o( . Sorry abuot that. Here it is:

        static void WinEventProc(
            IntPtr hWinEventHook, uint eventType,IntPtr hwnd, int idObject, int idChild,
            uint dwEventThread, uint dwmsEventTime)
        {
            if (eventType == EVENT_SYSTEM_FOREGROUND)
            {
                int pid;
                Process[] pc = Process.GetProcessesByName("explorer");
                uint processID = 0;
                uint threadID = GetWindowThreadProcessId(hwnd, out processID);

                foreach (Process p in pc)
                {
                    if (p.Id == processID) m_explHwnd = hwnd;
                }
            }
        }

Open in new window