Extract icon from an application

Asked

Viewed 412 times

3

I need to get the app icon that is open on the screen. App icons, browsers (examples pick up the Chrome icon , word icon and so on...)

I tried to do so:

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);

            } 

However I am having the following error(Only a part of a Readprocessmemory or Writeprocessmemory request has been completed).

Does anyone have any idea??

  • What application is this? Explain your problem better.

  • I ran this code here and the errors I got were denied access. I didn’t get the error you described. By ignoring errors (Try-catch, with empty catch), I was able to get all possible icons from my applications.

  • Face mine is not rolling and continues with the mistake I described above.

  • Is terminating your Filestream after using?

  • Open the file of manifesto, if you don’t have a, and insert the <requestedExecutionLevel level="requireAdministrator" /> line inside the node trustInfo/security/requestedPrivileges

1 answer

1

I believe that eventually some processes did not allow to extract the icon from them.

What I did was add one try catch to continue execution in the event of a 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 the Platform target for x64

Browser other questions tagged

You are not signed in. Login or sign up in order to post.