/// <summary>
/// 更改快捷方式文件图标
/// </summary>
/// <param name="initialSource"></param>
/// <param name="iconLocation"></param>
public async static void ChangeLinkIcon(string initialSource, string iconLocation)
{
// initialSource = @"C:\Users\Public\Desktop\微信.lnk"; //获取要更改的快捷方式路径
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(initialSource); //获取快捷方式对象
//shortcut.Description = "这是修改后的备注信息!"; //修改备注,鼠标放在图标上提示改文字 (右键快捷方式的备注(O))
shortcut.IconLocation = iconLocation;// @"C:\Users\Administrator\Desktop\2.ico";
//其他属性也是如此
shortcut.Save();
await Task.Delay(500);
}
/// <summary>
/// 获取快捷方式路径
/// </summary>
/// <param name="shortcutFilePath"></param>
/// <returns></returns>
static string GetShortcutTargetPath(string shortcutFilePath)
{
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutFilePath);
string targetPath = shortcut.TargetPath; // 获取快捷方式的目标路径
return targetPath;
}
private void SelectShortcut_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "快捷方式文件|*.lnk"; // 仅显示快捷方式文件
if (openFileDialog.ShowDialog() == true)
{
string shortcutPath = openFileDialog.FileName;
string targetPath = GetShortcutTargetPath(shortcutPath);
Console.WriteLine("快捷方式的目标路径是: " + targetPath);
TbkExePath.Text = shortcutPath;
}
}
private void SelectShortcutIcon_Click(object sender, RoutedEventArgs e)
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "ico files (*.ico)|*.ico|All Files(*.*) | *.* ";
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == true)
{
string selectedFileName = dlg.FileName;
string saftFileName = dlg.SafeFileName;
TbkIconPath.Text = selectedFileName;
}
else return;
}
catch (Exception ex)
{
}
}
private void SetShortcutIcon_Click(object sender, RoutedEventArgs e)
{
ChangeLinkIcon(TbkExePath.Text, TbkIconPath.Text);
}
WPF修改桌面快捷方式图标
最新推荐文章于 2024-10-23 09:49:03 发布
本文介绍了如何在Windows中使用C#编程语言更改快捷方式文件的图标以及获取其目标路径,包括ChangeLinkIcon方法和相关辅助函数的实现。
摘要由CSDN通过智能技术生成