Extract icon from an application

2

I need to get the applications icon that is open on the screen. Applications icons, browsers (examples take the chrome icon, word icon and so on ...)

I tried to do this:

Process[] listProcesses = Process.GetProcesses();
        foreach (Process p in listProcesses)
        {
            Icon icon = Icon.ExtractAssociatedIcon(mo.MainModule.FileName);
            string uri = @"C:\imagens\" + p.ProcessName + ".ico";

            if (!File.Exists(uri))
            {

                FileStream stream = new FileStream(uri, FileMode.CreateNew);
                icon.Save(stream);

            } 

But I'm having the following error (Only a part of a ReadProcessMemory or WriteProcessMemory request has completed).

Does anyone have an idea ??

    
asked by anonymous 25.05.2016 / 14:23

1 answer

0

I believe that some processes could not extract the icon from them.

What I did was add try catch to continue execution in case of failure.

Remembering that you will have to run the application with administrator permissions.

class Program
{
    static void Main(string[] args)
    {
        if (Elevate())
        {
            Process[] listProcesses = Process.GetProcesses();
            foreach (Process p in listProcesses)
            {
                try
                {
                    Icon icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
                    string uri = @"C:\imagens\" + p.ProcessName + ".ico";

                    if (!File.Exists(uri))
                    {
                        FileStream stream = new FileStream(uri, FileMode.CreateNew);
                        icon.Save(stream);
                        stream.Close();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(p.ProcessName + " - " + ex.Message);
                }
            }

        }
        else
        {
            Console.WriteLine("Execute o app com privilégios elevados");
        }

        Console.ReadKey();
    }

    public static bool Elevate()
    {
        WindowsIdentity user = WindowsIdentity.GetCurrent();

        WindowsPrincipal role = new WindowsPrincipal(user);

        if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
        {
            return false;
        }

        if (user == null)
        {
            return false;
        }

        return role.IsInRole(WindowsBuiltInRole.Administrator);
    }
}

Another point I changed was Platform target to x64

    
20.10.2016 / 15:11