您的位置:首页 > 编程语言 > C#

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

2014-03-06 14:49 756 查看
 *****

心情:各种百度,各种搜,搞了老半天,真不容易 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);
}

}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐