Solved

get the path of an opened window before paste

Posted on 2011-03-07
10
596 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
  • 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now