13
FileInfo fi = new FileInfo(fileToExcecute);
Directory.SetCurrentDirectory(fi.DirectoryName);

ProcessStartInfo pInfo = new ProcessStartInfo();
pInfo.FileName = fileToExcecute;
pInfo.RedirectStandardOutput = false;
pInfo.RedirectStandardError = false;
pInfo.CreateNoWindow = false;
pInfo.UseShellExecute = true;
pInfo.WorkingDirectory = fi.DirectoryName;
if (runas)
    pInfo.Verb = "runas";
pInfo.WindowStyle = ProcessWindowStyle.Normal;

Process p = Process.Start(pInfo);

The application icon is missing from the taskbar. It's just a blank square!

The above code works fine for several projects however fails with one specific program - lets call it projectX.exe. I have re-written the Main as well as startup methods of projectX.exe so that they mimic another project that displays its icon fine.

I have tried for days to discover why but have failed dismally. I have tried various ideas including changing the icon, changing it at runtime, as well as toggling whether it should be displayed or not.

If I require that projectX.exe be run as administrator, the icon displays fine but that option is not available to my clients.

Edit 20 Oct 2017 If I change the name of 'projectX.exe' to something else for example 'test.exe', then the icon shows OK in the taskbar. Where are the icons for a program stored in the registry?

Edit 22nd October 2017 After refreshing the icons as suggested, when running the program from File Explorer or creating a shortcut, the icon is no longer displayed in the taskbar.

Edit 12th November 2017 Running the program 'As Administrator', the icon displays in the taskbar as expected.

David
  • 958
  • 2
  • 12
  • 29
  • 1
    What about Session 0 isolation? https://msdn.microsoft.com/en-us/library/bb756986.aspx. Maybe it's not exactly your case but something similar. – user2250152 Oct 12 '17 at 06:08
  • 3
    Google "reset shell icon cache". – Hans Passant Oct 12 '17 at 07:03
  • @HansPassant I renamed all the iconcache files and then rebooted. New iconcache files were created however the problem described persists. – David Oct 13 '17 at 05:17
  • @user2250152 I have to agree but I'm unable to construct a suitable c# method to try it out. I didn't want to spend to much time on that avenue because it fails to explain why every other program (fileToExecute) called by the same method works fine. – David Oct 13 '17 at 05:23
  • clearly, the cause would be in ProjectX, so I suggest you deconstruct or reconstruct ProjectX until the phenomenon dis - or re appears. Maybe then you'll have a better idea what the cause could be. – nico boey Oct 15 '17 at 13:51
  • @nicoboey I have already done this as well as created two other 'startup programs' but all do the same thing. – David Oct 15 '17 at 20:57
  • Did you try running the projectX.exe from the console, ex: `start projectX.exe`. Are you sure its related to `Process.Start`? Can you make minimal sample with the issue? – Jawad Al Shaikh Oct 19 '17 at 03:50
  • @jawadalshaikn I have tried everything I can think of. The problem is that every program i use to test the problem with works fine. – David Oct 19 '17 at 11:48
  • Reading through the whole QA and comments, I suspect that while troubleshooting: more than single change being performed in one step! logically @HansPassant answer seems to explain it all. So if my assumption valid, please try performing `single change at a time` couple of times and document the results, then move on based on your troubleshooting skills. – Jawad Al Shaikh Oct 22 '17 at 05:29

3 Answers3

7

If I change the name of 'projectX.exe' to something else ... then the icon shows OK.

This is definitely an icon cache induced problem. It is not very clear why resetting it did not help to solve this problem, but it looks like you did it by hand and that has ways of not panning out correctly.

Some background. This problem is pretty common on a dev machine, programmers tend to take care of the chrome only after getting their program debugged and tested. Explorer got to see their program.exe file with the wrong icon and copied that into its cache. Changing the .exe does not force it to refresh the cached copy, arguably a bug. The cache is otherwise pretty important for Explorer, digging the icons out of the files on a folder view full of files can take any easy handful of seconds on a spindle drive.

The cache is stored in a file, not the registry. You'll find it back in c:\users\yourname\appdata\local\iconcache.db, beware that it is a hidden file. Windows 8 and up use a much fancier caching scheme with multiple icon*.db files, stored in the c:\users\yourname\appdata\local\microsoft\windows\explorer directory.

Deleting these files is enough to force Explorer to re-create them. But that doesn't necessarily come a good end, you can only be 100% sure that Explorer creates a fresh copy by terminating it before you delete the files. And other processes may have a lock on these files if they have the cache file open while you are doing this, typically because they have a shell extension loaded.

I think the best way to reset the cache is by using Ramesh Srinivasan's cleariconcache.vbs script, available from this web page. His VBScript code looks convincingly right, taking care of all the corner-cases and dutifully reporting failure. Close all running programs to give it maximum odds for success.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Unfortunately, if it is an icon cache induced problem, clearing the icon cache is not the total solution. I have done all you ask however the end result is still a blank square in the taskbar. This leads me to believe that the cause of the blank icon is something to do with my program's startup methodology. This doesn't answer why changing the program's name 'fixes' the problem. I have changed the icon in the program as well as changed the name of the InstallAware installer program to try to see if two progarms can 'share' the same icon but that didn't work either. – David Oct 22 '17 at 00:08
  • I've awarded this answer the bounty as its about to expire however the issue remains unsolved. – David Oct 22 '17 at 21:30
  • For anyone looking for the answer, I found one that works here: https://stackoverflow.com/a/10825808/602447 – Alberto Rechy Nov 30 '21 at 17:01
1

The issue is very hard to diagnose without a full understanding of your environment.

This does however sound like it could well be an operating system issue rather than a problem with your code.

One option might be to programatically clear the icon cache whilst restarting explorer.exe the following code should do this:

foreach (Process exe in Process.GetProcesses())
{
     if (exe.ProcessName.StartsWith("iexplore"))
         exe.Kill();
     }

     // clear icon cache
     strCmdText= "del %userprofile%\appdata\local\iconcache.db /a ";
     Process.Start("CMD.exe",strCmdText);

     Process.Start("explorer.exe");
}

Your icon should hopefully be visible now.

K-Dawg
  • 3,013
  • 2
  • 34
  • 52
  • I'll give it another try, however I should have mentioned that this issue is revealing itself on many machines. I'll try on a few versions of Windows to see if I can isolate the issue. – David Oct 15 '17 at 21:45
  • Killing the process and then trying to delete the several *.db files proved cumbersome and really requires a reboot to be successful. Nevertheless, even that didn't fix the problem. – David Oct 19 '17 at 02:50
  • I will keep thinking about it and if anything comes to mind I will amend this. – K-Dawg Oct 19 '17 at 06:26
  • Are you able to upload a screenshot? – K-Dawg Oct 19 '17 at 06:26
  • also does this only happen when debugging and not when running the release build manually? – K-Dawg Oct 19 '17 at 06:31
  • have you tried also playing with (changing) the exe directory to see if it might be permissions on it's directory? – K-Dawg Oct 19 '17 at 06:33
  • 1
    @primenbydesign There are 3 programs that are run from the above quoted code. I have written two additional specific programs called by the same code and they all work fine. I'll do a sceen capture but it just shows a blank square. Thank you for your thoughts – David Oct 19 '17 at 11:54
0

We had the exact same issue, but in our case it turns out our icon was too big (in kb). Once we made it smaller in kb, the issue was resolved!