C# 在最顶端打开的Windows资源管理器中创建新文件夹
我正在编写一个控制台应用程序。 它必须做到以下几点C# 在最顶端打开的Windows资源管理器中创建新文件夹,c#,C#,我正在编写一个控制台应用程序。 它必须做到以下几点 我将在窗口的任务栏中放置此应用程序的(pin)EXE 我将打开windows资源管理器,否则可能已经打开了许多 以前的窗户 我将选择要在其中创建文件夹的窗口 (文件夹层次结构) 我将单击我的EXE任务栏上的图标 在步骤1中固定 我尝试了以下代码 当我使用windows调度程序对其进行调度时,它正在工作。每5分钟后,将在最顶部的窗口中创建一个新文件夹 但是,当我尝试在将exe固定到任务栏后执行exe时,它不起作用 如果有什么解决办法,请告诉我 注
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);
}
}
要从COM添加的引用
C:/windows/system32/Shell32
和Microsoft Internet Controls
。此代码适用于我,只添加了两行。在执行更改之前,返回上次访问的窗口
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace lastActiveWindow
{
internal class Program
{
private static void Main(string[] args)
{
// New lines..
SendKeys.SendWait("%{Tab}");
Thread.Sleep(500);
try
{
// get the active window
var 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;
var Buff = new StringBuilder(nChars);
//if (GetWindowText(handle, Buff, nChars) > 0)
//{
// return Buff.ToString();
//}
}
else
{
Directory.CreateDirectory(currentFolder.Path + "//NewFolder//Db_Scripts");
Directory.CreateDirectory(currentFolder.Path + "//NewFolder//Documents");
Directory.CreateDirectory(currentFolder.Path + "//NewFolder//SourceCode");
// return currentFolder.Path;
}
}
break;
}
}
}
catch (Exception ex)
{
Console.Write(ex.ToString());
Console.Read();
}
//Console.Read();
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
}
}
使用“自动热键”进行操作更简单,您可以创建一个热键来执行操作。@C1sc0抱歉。没有使用外部工具的权限可能在你点击任务栏时会失去焦点,你可以为自己制作一个类似自动热键的应用程序(),或者将你的小应用程序放在上下文菜单上(只需在注册表中添加一行)在运行应用程序逻辑
SendKeys.Send(“%{Tab}”)之前,尝试模拟Alt+选项卡
这将更改回上一个活动窗口。一个更好的解决方案是弹出一个文件夹选择对话框,让用户选择创建新文件夹的位置。为什么需要使用资源管理器窗口?