Where does Windows store desktop icon positions

Save and Restore Icon Positions on Desktop

Rate me:
Please Sign up or sign in to vote.
4.93/5 [18 votes]
CPOL5 min read
The article describes how to save and restore positions of icons on Windows desktop.
  • Download source code - 181.9 KB

Introduction

Recently, my company gave me a dock station for my laptop. It is a nice toy to play with, but soon I faced a little problem. From time to time [actually rather frequently] after reboot, my laptop forgets screen resolution. It does not bother me too much because restoring screen resolution is fast and easy. But the problem is that with resolution, it forgets positions of all icons on my desktop. It is easy to place them again, but not so fast as I have quite a lot of icons.

Of course, there are programs like IconRestorer which can help me. But after all, we are programmers and this is an interesting task to solve. So let the journey begin!

Registry

I Googled where Windows stores information about positions of icons on desktop. And there were nice and easy answers that this information is stored in the registry key "CurrentUser\Software\Microsoft\Windows\Shell\Bags\1\Desktop". I knew how to work with registry and it was an easy task. But very soon I realized that it is not a correct answer. Looks like Windows reads this information on LogIn and stores in on LogOut. And I'd like to restore positions of icons without rebooting my computer. So I needed another solution.

PInvoke

I Googled again and found that actually desktop is a ListView control inside some process. And anyone can send messages to this control using standard SendMessage API of Windows. And get some results. But it means that I should use PInvoke technology to work with legacy Windows API. Personally, I felt uneasy about PInvoke. First of all, you should know in which DLL each required function lives. Then there are different marshaling issues. I was scared about it. But fortunately, I found a great site: PInvoke.net. It is very easy to use and contains .NET wrappers for many native Windows functions and structures including some useful overloads. I am definitely going to use this site later on and hope it will help you too.

Process of Desktop

To be able to send messages to a control, one should get its handle - unique id of the control in Windows. In order to do it, one should first identify process containing this control. And here, I faced the first problem. You see, Windows can host the control of desktop in different processes. If you have one static image on desktop, you have one process. If you have several images and Windows changes background image from time to time, then you have another process. It took me some time to find a generic algorithm which should get handle of desktop control here.

Setting Positions of Icons

As it was said, icons are just items displayed into ListView control. It is very easy to get total number of these items:

C#
Copy Code
var numberOfIcons = [int]Win32.SendMessage[_desktopHandle, Win32.LVM_GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero];

It is also easy to set position of an icon to some point [x,y]:

C#
Copy Code
public static IntPtr MakeLParam[int wLow, int wHigh] { return [IntPtr][[[short]wHigh

Chủ Đề