I am writing a console application. it has to do the following
I'll place(pin) EXE of this application in Task-bar of the window.
I'll open windows explorer or there may be already many opened windows before.
I'll select a window where I want to create a folder (folder hierarchy)
I'll click on the icon from Task-bar of my EXE pinned in step 1.
I have tried following code.
It is working when I am scheduling it by using windows scheduler. after every 5 minutes a new folder will be created in the topmost window.
However when I am trying with executing exe after pinning it to taskbar, it is not working..
Please let me know if there is any solution.
NOTE - Helpful if you tell me how to create a new option of Create New Custom Folder after right click
using System;
using System.Text;
namespace CreateNewFolders
{
class Program
{
static void Main(string[] args)
{
try
{
// get the active window
IntPtr handle = GetForegroundWindow();
// Required ref: SHDocVw (Microsoft Internet Controls COM Object) - C:\Windows\system32\ShDocVw.dll
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
// loop through all windows
foreach (SHDocVw.InternetExplorer window in shellWindows)
{
Console.WriteLine(((int)handle).ToString());
Console.WriteLine(window.HWND.ToString());
if (window.HWND == (int)handle)
{
// Required ref: Shell32 - C:\Windows\system32\Shell32.dll
var shellWindow = window.Document as Shell32.IShellFolderViewDual2;
// will be null if you are in Internet Explorer for example
if (shellWindow != null)
{
// Item without an index returns the current object
var currentFolder = shellWindow.Folder.Items().Item();
// special folder - use window title
// for some reason on "Desktop" gives null
if (currentFolder == null || currentFolder.Path.StartsWith("::"))
{
// Get window title instead
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
//if (GetWindowText(handle, Buff, nChars) > 0)
//{
// return Buff.ToString();
//}
}
else
{
System.IO.Directory.CreateDirectory(currentFolder.Path + "//NewFolder//Db_Scripts");
System.IO.Directory.CreateDirectory(currentFolder.Path + "//NewFolder//Documents");
System.IO.Directory.CreateDirectory(currentFolder.Path + "//NewFolder//SourceCode");
// return currentFolder.Path;
}
}
break;
}
}
}
catch (Exception ex)
{
Console.Write(ex.ToString());
Console.Read();
}
//Console.Read();
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
}
}
References to be added C:/windows/system32/Shell32
and Microsoft Internet Controls
from COM.