create shortcut

Searching the Source Code of Adding my Program Shortcut into Start Menu

Now, I am searching the source code by C# of adding my program shortcut into Start Menu. I think its solution maybe is to modify the correct key in the registry. And I have acquired knowledge about the the class library “Microsoft.Win32” which includes some methods to handle the registry. I believe I can find the perfect sollution at last! :) Maybe.

Oops... now,  I haven't get some information correlative with this issue. So, if somebody else will tell me the answer directly, even give me some source code, It is very nice! I will appreciate his or her help:) 

/*Modified on October 03,2004*/

-------*******************************************************-------------------------------

ps: OK! I have found the answer already! It's great and very simple. But it make me embarrassed what I guessed before was definitely wrong. We need not handle the registry, in fact, there are some convenient methods in .Net.

There is solution as bellow:

using IWshRuntimeLibrary;

//folderType is special system folder type;
//linkName is shortcut's title which you want to add;
public bool AddShortCut(Environment.SpecialFolder folderType,string targetDir,string linkName)
{
       try
       {
             string folder = Environment.GetFolderPath(folderType) + "\\" + linkName;
             // Create a Windows Script Host Shell class
             IWshShell_Class shell = new IWshShell_ClassClass();
            // Define the shortcut file
            IWshShortcut_Class shortcut = shell.CreateShortcut(folder + ".lnk") as IWshShortcut_Class;
            shortcut.TargetPath = targetDir;
            shortcut.Save();   
            return true; 
       }
       catch (Exception ex)
       {
               return false;
        }     
}

If you want to add the shortcut to StartMenu's Program menu, you can invoke the AddShortCut() method like this:

AddShortCut(Environment.SpecialFolder.Programs);

你可能感兴趣的:(C++,c,.net,windows,Microsoft)