1

我正在编写一个 C# 程序,它侦听文件资源管理器中按下的空格。当按下空格时,我创建一个 WPF 窗口,它在 WPF 窗口中显示选定的文件。

唯一的问题是,当有人在编辑文件名并按下空格以获取文件名中的空格时,也会调用此文件 Preview。

有没有办法检测用户是否正在重命名文件?


附加信息:

我知道我可以听 F2,但也可以通过用鼠标左键单击两次或右键单击文件并选择重命名来开始重命名文件。所以这不是一个好的解决方案。

技术信息(如果需要):

我使用 GetForegroundWindow 来检查前景中的窗口是否是资源管理器窗口。然后我使用 Hooks 在前台的资​​源管理器进程中监听按下的键。

要获得选定的项目,我使用 SHDocVw 和 Shell32

                    Shell32.FolderItems selectedItems = ((Shell32.IShellFolderViewDual2) window.Document).SelectedItems();
4

2 回答 2

0

要检测文件重命名,请检查 FileSystemWatcher.Changed 事件。这是取自 MSDN 的示例代码。MSDN FileSystemWatcher 示例

我稍微修改了代码。我已经验证了代码。它通知文件何时重命名。

using System;
using System.IO;
using System.Security.Permissions;

namespace FileWatcher
{
    class Program
    {
        static void Main(string[] args)
        {
            Run();
        }

        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Run()
        {
            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path = @"c:\temp"; //Specify the directory name where file resides

            /* Watch for changes in LastAccess and LastWrite times, and
               the renaming of files or directories. */

            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            // Only watch text files.
            watcher.Filter = "*.txt";

            // Add event handlers.
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            // Begin watching.
            watcher.EnableRaisingEvents = true;

            // Wait for the user to quit the program.
            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
        }

        // Define the event handlers. 
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            // Specify what is done when a file is renamed.
            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
        }
    }
}
于 2015-02-03T08:34:09.380 回答
0

您的文件导航增强不应该冒干扰 Windows 资源管理器中当前存在的行为的风险。

因此:

Ctrl+Space

在文件重命名操作期间不会触发诸如Ctrl+之类的新命令,而且您的应用程序将使用Windows 操作系统用户命令中烘焙的标准(带有一点香料)工作。Space

  • Ctrl+Space是我们开发人员的 IntelliSense 命令,因此它是人们用于“更多信息”/“帮助我”的自然命令。

我希望你有一个非常特别的文件预览。在过去的 2 年中,有很多案例表明 PITA 不是一种乐趣:(
http://support.microsoft.com/kb/983097
http://support.microsoft.com/kb/2257542

于 2015-02-03T10:03:44.757 回答