On windows, different icons are displayed for different file extensions. However, if your program uses a file extension defined by yourself (for example. guo), when windows finds it as "unknown type", a default unknown type icon is displayed, which is neither easy to see nor easy to find your files in a pile of files.
Therefore, this article describes how to use the registry to change the default icon of a file.
Take the. guo type as an example:
In C #, you only need to execute:
Registry.SetValue(, , );Registry.SetValue(, , );Registry.SetValue(, , ); Registry.SetValue(, , );
You can create a default icon and a default associated program for the. guo file. MyGuo is the alias of. guo.
In command, % 1 indicates the input file name. For example, if you double-click the D: \ bin \ a. guo file, it is equivalent to executing the following command line:
D:\bin\Guo.exe “D:\bin\a.guo”
In this example, when guo.exe is started, the file path can be obtained through the args parameter in static void Main (string [] args.
After you run the above Code, you will find that the. guo file icon in Explorer has not changed, because you need to restart the Explorer or the computer to take effect. Is there any way to take effect in real time without restarting? Yes, we use the API to implement:
[System.Runtime.InteropServices.DllImport( SHChangeNotify( wEventId, uFlags, IntPtr dwItem1, IntPtr dwItem2);
Then add this sentence after the above Code:
SHChangeNotify(, , IntPtr.Zero, IntPtr.Zero);
What do you see?