编程(C#)实现创建 internet快捷方式 文件

 *****

心情:各种百度,各种搜,搞了老半天,真不容易 a

推荐解决方案2

貌似似这个也不错:http://xiaochen.2003.4.blog.163.com/blog/static/48040967201253033250671/

解决方案1:

加载com组件:【引用】右键-->添加引用-->com组件--->选择“Windows Script Host Object Model”

引用命名空间:using IWshRuntimeLibrary; 

调用如下函数即可

        private void CreateShortcutFile(string Title, string URL, string SpecialFolder)
        {
            // Create shortcut file, based on Title
            System.IO.StreamWriter objWriter = System.IO.File.CreateText(SpecialFolder + "//" + Title + ".url");
            // Write URL to file
            objWriter.WriteLine("[InternetShortcut]");
            objWriter.WriteLine("URL=" + URL);
            // Close file
            objWriter.Close();
        }

 解决方案2:

 

        /// <summary>  
        /// 添加收藏夹  
        /// </summary>  
        /// <param name="url">对应的网页的url</param>  
        /// <param name="saveName">保存的名称</param>  
        /// <param name="folderName">文件夹名称</param>  
        public static void AddToFavorites(String url, String saveName, String folderName)
        {
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(url));
            request.Method = "GET";
            request.Timeout = 10000;
            try
            {
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    //获取当前用户的收藏夹的物理文件夹位置  
                    String favoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
                    String savePath = favoritesPath;
                    if (!String.IsNullOrEmpty(folderName))
                    {
                        savePath += @"/" + folderName;
                        if (!Directory.Exists(savePath))
                            Directory.CreateDirectory(savePath);
                    }
                    IWshRuntimeLibrary.WshShell shell_class = new IWshRuntimeLibrary.WshShellClass();
                    IWshRuntimeLibrary.IWshShortcut shortcut = null;
                    try
                    {
                        shortcut = shell_class.CreateShortcut(favoritesPath + @"/" +folderName+"/"+ saveName + ".lnk") as IWshRuntimeLibrary.IWshShortcut;
                        shortcut.TargetPath = url;
                        shortcut.Save();
                        MessageBox.Show("添加成功");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("添加失败");
                    }
                }
                else
                {
                    MessageBox.Show("请求失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值