ZergaKila

Does any one know how to get info about icons on the desktop

I want to get a list of the icons on the desktop

the parameters i need are [Icon Text, Icon Image,icon position on desktop,icon target(a file or my computer or my documents...) ,size, and as much information as possible]

thanks for any help



Re: Windows Forms General How can i get desktop's icon's information ?

Eli Gazit

To get the icons from any file (and from the desktop), use Icon.ExtractAssociatedIcon(fileName). To get the Desktop path, GetFolderPath() method of Environment.

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

string[] files = Directory.GetFiles(path);

Icon fileIcon = Icon.ExtractAssociatedIcon(fileName);






Re: Windows Forms General How can i get desktop's icon's information ?

nobugz

Check the ResolveShellLink() method in this thread to get info about a .lnk file. Note that the desktop is made up from the contents of the user's Desktop folder as well as the All Users' folder. Environment.GetSpecialFolder() has a glaring omission, you can't get the path to the All Users desktop. You can solve this through the Shell32 ActiveX object too:

Shell32.Shell shl = new Shell32.ShellClass();
Shell32.Folder folder = shl.NameSpace(0x19);
foreach (Shell32.FolderItem item in folder.Items())
Console.WriteLine(item.Name);






Re: Windows Forms General How can i get desktop's icon's information ?

ZergaKila

Thanks but I alrady succeed to get the files list

I need to find:

1.Icon position on the desktop

2.Icon Image+Position of special Icons like "My Computer" and "Network Connections"





Re: Windows Forms General How can i get desktop's icon's information ?

Zhi-Xin Ye - MSFT

I will write a sample for you to demonstrate how to get the desktop icon locations.
First, actually the desktop is a large ListView, if you use SPY++ to see the handle of the desktop, you will find that there is a window with the title Program Manager, under which is another window of SHELLDLL_DefView type without title, below it is a window of SysListView32 type with caption of FolderView. So, we can use the FindWindow and FindWindowEx API to get the ListView which host the desktop icons, and use SendMessage API to send LVM_GETITEMPOSITION message to get the icon positions, however, since each process has its own memory, we can't steal program's memory directly from one process to another, we need some trick, we can allocate memory in the address space of the desktop ListView and read from that, this can be done by VirtualAllocEx() API. Now, the problem is to get the process handle of the ListView, we can use GetWindowThreadProcessId API to get the PID of the desktop window process, and use OpenProcess() to get a handle to the process.
Below is my sample





Re: Windows Forms General How can i get desktop's icon's information ?

Zhi-Xin Ye - MSFT


Code Snippet

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

this.listView1.Columns.Add("Icon Name");

this.listView1.Columns.Add("Icon Location");

this.listView1.View = View.Details;

}

public const uint LVM_FIRST = 0x1000;

public const uint LVM_GETITEMCOUNT = LVM_FIRST + 4;

public const uint LVM_GETITEMW = LVM_FIRST + 75;

public const uint LVM_GETITEMPOSITION = LVM_FIRST + 16;

public const uint PROCESS_VM_OPERATION = 0x0008;

public const uint PROCESS_VM_READ = 0x0010;

public const uint PROCESS_VM_WRITE = 0x0020;

public const uint MEM_COMMIT = 0x1000;

public const uint MEM_RELEASE = 0x8000;

public const uint MEM_RESERVE = 0x2000;

public const uint PAGE_READWRITE = 4;

public const int LVIF_TEXT = 0x0001;

[DllImport("kernel32.dll")]

public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,

uint dwSize, uint flAllocationType, uint flProtect);

[DllImport("kernel32.dll")]

public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,

uint dwSize, uint dwFreeType);

[DllImport("kernel32.dll")]

public static extern bool CloseHandle(IntPtr handle);

[DllImport("kernel32.dll")]

public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,

IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);

[DllImport("kernel32.dll")]

public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,

IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);

[DllImport("kernel32.dll")]

public static extern IntPtr OpenProcess(uint dwDesiredAccess,

bool bInheritHandle, uint dwProcessId);

[DllImport("user32.DLL")]

public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

[DllImport("user32.DLL")]

public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);

[DllImport("user32.DLL")]

public static extern IntPtr FindWindowEx(IntPtr hwndParent,

IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll")]

public static extern uint GetWindowThreadProcessId(IntPtr hWnd,

out uint dwProcessId);

public struct LVITEM

{

public int mask;

public int iItem;

public int iSubItem;

public int state;

public int stateMask;

public IntPtr pszText; // string

public int cchTextMax;

public int iImage;

public IntPtr lParam;

public int iIndent;

public int iGroupId;

public int cColumns;

public IntPtr puColumns;

}






Re: Windows Forms General How can i get desktop's icon's information ?

Zhi-Xin Ye - MSFT

Code Snippet


private
void button1_Click(object sender, EventArgs e)

{

// get the handle of the desktop listview

IntPtr vHandle = FindWindow("Progman", "Program Manager");

vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);

vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", "FolderView");

//Get total count of the icons on the desktop

int vItemCount = SendMessage(vHandle, LVM_GETITEMCOUNT, 0, 0);

this.label1.Text = vItemCount.ToString();

uint vProcessId;

GetWindowThreadProcessId(vHandle, out vProcessId);

IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ |

PROCESS_VM_WRITE, false, vProcessId);

IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 4096,

MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

try

{

for (int j = 0; j < vItemCount; j++)

{

byte[] vBuffer = new byte[256];

LVITEM[] vItem = new LVITEM[1];

vItem[0].mask = LVIF_TEXT;

vItem[0].iItem = j;

vItem[0].iSubItem = 0;

vItem[0].cchTextMax = vBuffer.Length;

vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVITEM)));

uint vNumberOfBytesRead = 0;

WriteProcessMemory(vProcess, vPointer,

Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),

Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead);

SendMessage(vHandle, LVM_GETITEMW, j, vPointer.ToInt32());

ReadProcessMemory(vProcess,

(IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVITEM))),

Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),

vBuffer.Length, ref vNumberOfBytesRead);

string vText = Encoding.Unicode.GetString(vBuffer, 0,

(int)vNumberOfBytesRead);

string IconName = vText;

//Get icon location

SendMessage(vHandle, LVM_GETITEMPOSITION, j, vPointer.ToInt32());

Point[] vPoint = new Point[1];

ReadProcessMemory(vProcess, vPointer,

Marshal.UnsafeAddrOfPinnedArrayElement(vPoint, 0),

Marshal.SizeOf(typeof(Point)), ref vNumberOfBytesRead);

string IconLocation = vPoint[0].ToString();

//Insert an item into the ListView

this.listView1.Items.Add(new ListViewItem(new
string[]{IconName,IconLocation}));

}

}

finally

{

VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);

CloseHandle(vProcess);

}

this.listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

}

}






Re: Windows Forms General How can i get desktop's icon's information ?

Eli Gazit

Zhi-Xin

Great Stuff!!

This post contains a code sample allright....






Re: Windows Forms General How can i get desktop's icon's information ?

Leviathan3328

Awsome stuff Zhi-Xin I was also looking for a way to do this. The only thing I dont think you didnt cover was how to capture the actual Icon for each item on the desktop, expecialy for the special icons on the desktop like My Computer, Network Neighbourhood and the Recycle Bin. How would we do this, is this even posible to do





Re: Windows Forms General How can i get desktop's icon's information ?

Zhi-Xin Ye - MSFT

The Icons of "My Computer","My Document" etc are in the shell32.dll which locates in c:\windows\system32\shell32.dll, for how to retrive them, see this project: Retrieving shell icons




Re: Windows Forms General How can i get desktop's icon's information ?

Jarek Stachowiak

Can I use LVM_GETIMAGELIST to import icons from Desktop to C# Or, is there any other ellegant way

If yes, please explain.