The issue is really how you are creating that file. You can’t just take a .gif or .png file and change the extension. There is a free plugin for Photoshop for creating .ico files, and also some online sites that generate them for you. E.g.
I have noticed that when I download a Favicon and add it to a folder on my computer I can see he image as a small preview however I couldn’t with the original Favicon I had.
Do I need to use any specific code, would this work?
That code is ok, but just make sure the favicon is placed in your root folder. Also, it sometimes takes a while for the fav to show up. It helps if you bookmark your site in your browser.