Stevoisiak
Stevoisiak

Reputation: 26762

How do I monitor when a file icon on the Desktop is moved in C#?

I want to detect when a file icon on the Desktop is moved to a different position. (My goal is to counteract a Windows 11 bug where renamed icons move themselves.)

I wrote a C# script which uses a FileSystemWatcher to monitor the Desktop folder for file Changed events. However, it doesn't trigger when an icon is moved. The documentation for FileSystemWatcher doesn't mention any method of tracking icon movement, leading me to believe I need to take a different approach.

using var watcher = new FileSystemWatcher(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
watcher.EnableRaisingEvents = true;
// Detects when a file is changed
// FIXME: Doesn't track when desktop icons are moved.
watcher.Changed += OnChanged;

// Monitor until user exits
Console.WriteLine($"Monitoring {watcher.Path} for changes. Press enter to stop monitoring.");
Console.ReadLine();

void OnChanged(object sender, FileSystemEventArgs e)
{
    Console.WriteLine($"'{e.Name}' was changed.");
}

How can I monitor when a file icon on the Desktop is moved using C#?

Upvotes: 0

Views: 40

Answers (0)

Related Questions