Solved

get the path of an opened window before paste

Posted on 2011-03-07
10
614 Views
Last Modified: 2013-12-17
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\
0
Comment
Question by:systan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 14

Author Comment

by:systan
ID: 35066457
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;
}
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 35069176
ShellBrowserWindow has HWND property. You could compare handle from GetForegroundWindow with it.
0
 
LVL 14

Author Comment

by:systan
ID: 35070464
Don't know how to apply, please help, a code snippet would be great.
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 14

Author Comment

by:systan
ID: 35090025
GetForegroundWindow Can't handle it, even GetLastActivePopup.
There must be a way to get the current or last folder opened.
0
 
LVL 14

Author Comment

by:systan
ID: 35091227
//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.
0
 
LVL 19

Accepted Solution

by:
Rimvis earned 250 total points
ID: 35092789
In this case, you will have to monitor windows and detect, when focus is changing. The problem with this approach is that you don't know, which window was active before you application was launched.

Here's sample code:

        delegate void WinEventDelegate(
            IntPtr hWinEventHook,uint eventType, IntPtr hwnd, int idObject,int idChild, 
            uint dwEventThread, uint dwmsEventTime);

        const uint WINEVENT_OUTOFCONTEXT = 0;
        const uint EVENT_SYSTEM_FOREGROUND = 3;

        [DllImport("user32.dll", SetLastError = true)]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
        [DllImport("user32.dll")]
        static extern bool UnhookWinEvent(IntPtr hWinEventHook);
        [DllImport("user32.dll")]
        static extern IntPtr SetWinEventHook(
            uint eventMin,uint eventMax, IntPtr hmodWinEventProc,WinEventDelegate lpfnWinEventProc, 
            uint idProcess, uint idThread, uint dwFlags);

        IntPtr m_hhook;
        WinEventDelegate _WinEventHook = new WinEventDelegate(WinEventProc);
        static IntPtr m_explHwnd = IntPtr.Zero;


....

        private void Form1_Load(object sender, EventArgs e)
        {
            m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND,
            EVENT_SYSTEM_FOREGROUND, IntPtr.Zero,
            _WinEventHook, 0, 0, WINEVENT_OUTOFCONTEXT);
        }

...

        private void cmdSomeTest_Click(object sender, EventArgs e)
        {
            string s = "x";

            //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)
            {
                if (shellWindow.HWND == (int)m_explHwnd)
                {
                    //This is the object that holds Explorer window information.
                    ShellFolderView view = (ShellFolderView)shellWindow.Document;
                    //Get ALL currently active windows path
                    Debug.Print(view.FocusedItem.Path);
                }
            }

            label1.Text = s;
        }
.....
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnhookWinEvent(m_hhook);
        }

Open in new window

0
 
LVL 14

Author Comment

by:systan
ID: 35093669
Error      1      The name 'WinEventProc' does not exist in the current context ?
0
 
LVL 14

Author Comment

by:systan
ID: 35096010
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
0
 
LVL 14

Author Closing Comment

by:systan
ID: 35100443
I think I got it,
thank you
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 35144777
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

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ModalPopup  question 22 53
VB.net Filesystem watcher not working 5 53
Visual Studio 2015 auto inserted code 12 85
advancedDataGridView / cancel / filter / using / button 5 23
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question