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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值