I'm programming a C# program which listens for Space pressed in the File Explorer. When space is pressed I create a WPF window which shows the selected File in a WPF Window.
The only problem is, that this file Preview is also called when someone is editing the filename and presses space for a whitespace in the filename.
Is there a way to detect if a user is renaming a file at the moment?
Additional Information:
I know that i could listen for F2, but there's also the way to start renaming a file by clicking two times with the left mouse button or by right clicking the file and selecting rename. So this would be no good solution.
Technical Information (if needed):
I use GetForegroundWindow to check if the Window in the foreground is a explorer window. Then i use Hooks to listen for the pressed Keys in the explorer process in foreground.
To get the selected Item i use SHDocVw and Shell32
Shell32.FolderItems selectedItems = ((Shell32.IShellFolderViewDual2) window.Document).SelectedItems();
To detect file rename, check for FileSystemWatcher.Changed Event. Here is the example code taken from MSDN. MSDN FileSystemWatcher Example
I have slightly modified code. I have verified the code. It notifies as and when file is renamed.
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);
}
}
}
Your file navigation enhancement should not risk interfering with the behaviour currently present in Windows Explorer.
Hence:
Ctrl + Space
Using a new command such as Ctrl + Space will not get triggered during a file-rename operation plus your application will work using the standard baked in Windows OS user-commands (with a little spice) .
I hope you have a very special File Preview. In the last 2 decades there have been a lot of cases where it has been a PITA not a pleasure :(
http://support.microsoft.com/kb/983097
http://support.microsoft.com/kb/2257542
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.