C#代码创建桌面快捷方式,win10系统报错,代码如下

云顶F 云顶F 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

static void Main(string[] args)
{
CreateDesktopLnkMethod(AppDomain.CurrentDomain.BaseDirectory + "favicon.ico", AppDomain.CurrentDomain.BaseDirectory + "test.exe", "测试2下");
Console.WriteLine("创建成功");
Console.ReadKey();
}

[DllImport("shfolder.dll", CharSet = CharSet.Auto)]
private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
private const int MAX_PATH = 260;
private const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019;
/// <summary>
/// 获取公共桌面路径
/// </summary>
/// <returns>返回桌面完整路径</returns>
public static string GetAllUsersDesktopFolderPath()
{
StringBuilder sbPath = new StringBuilder(MAX_PATH);
SHGetFolderPath(IntPtr.Zero, CSIDL_COMMON_DESKTOPDIRECTORY, IntPtr.Zero, 0, sbPath);
return sbPath.ToString();
}

/// <summary>
/// 创建桌面快捷方式,如已存在先删除再创建
/// </summary>
/// <param name="iconPath">icon图标路径</param>
/// <param name="ClientExeFullPath">程序启动路径,带*.exe</param>
/// <param name="lnkName">快捷方式名称</param>
public static void CreateDesktopLnkMethod(string iconPath, string ClientExeFullPath, string lnkName)
{
if (System.IO.File.Exists(iconPath))
{
WshShell shelllnk = new WshShell();
IWshShortcut shortcut = null;
try
{
string[] fi = System.IO.Directory.GetFiles(GetAllUsersDesktopFolderPath());
//string[] fi = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory));
foreach (var item in fi)
{
if (item.IndexOf(".lnk") > -1)
{
shortcut = (IWshShortcut)shelllnk.CreateShortcut(item);
if (shortcut.WorkingDirectory.Equals(AppDomain.CurrentDomain.BaseDirectory))
{//删除原有快捷方式
if (lnkName.Length <= 0)
{
lnkName = shortcut.FullName.Substring(shortcut.FullName.LastIndexOf("\\") + 1, shortcut.FullName.LastIndexOf(".") - shortcut.FullName.LastIndexOf("\\") - 1);
}
System.IO.File.Delete(item);
shortcut = null;
break;
}
}
shortcut = null;
}

//生成新的快捷方式
shortcut = (IWshShortcut)shelllnk.CreateShortcut(GetAllUsersDesktopFolderPath() + "\\" + lnkName + ".lnk");
shortcut.TargetPath = ClientExeFullPath;
shortcut.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
shortcut.WindowStyle = 1;
shortcut.Description = lnkName;
shortcut.IconLocation = iconPath;
shortcut.Save();
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
}
}
...全文

相关推荐

C#小程序,打开后会有一个Windows式的报错,恶搞性质

很简单的小程序,有源代码,但还不够完善,希望各位网友可以完善一下,让这个小程序变得更好玩,越恶搞越好!

aidyliuliu

C#客户端程序Visual Studio远程调试的方法详解

一,需求来源 在开发过程中,可能会要使用Win7 ,Win8 ,Win10等不同版本的系统去做兼容性调试,也有时候会去针对特别的显卡,无线网卡等等硬件设备的机器做优化,有一种较优的方案,那就是使用Visual Studio的远程调试功能,可以直接将测试机作为调试目标,直接跟踪断点和异常,可以像在本机调试一样,迅速的找到错误的地方。,而不需要在测试机安装VS环境。 >传统桌面客户端的远程调试相比UWP,ASP等项目来说,配置比较麻烦,因为它是非部署的应用程序,原理是复制编译的文件到远程计算机,通过网络来连接和VS的通信,本文主要讲述WPF,WinForm应用程序的远程调试# 二,正文 ①安装

weixin_38689824

c#使用process.start启动程序报错解决方法

c#使用process.start启动程序报错解决方法,大家参考使用吧

weixin_38731385

解决WIN10操作系统DataVisualization原因无法生成项目

使用Fastreport时,在Win10系统生成项目时总是提示此文件版本不一致的问题 DataVisualization, Version=3.5.0.0

playis

Win7系统微软TTS语音修复自测可用.rar

解决C# 的SpVoice voice = new SpVoice();报错问题,自测可用, 如果积分太高联系QQ492662461 修改下载积分 SpVoice 检索 COM 类工厂中 CLSID 为 {96749377-3391-11D2-9EE3-00C04F797396} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))。

qs1208
评论
收藏 已收藏
分享
返回顶部
云顶F
C# 111,129 成员 642,538 内容