网站框架图,中国空间站图片,2345网址导航官网官方电脑版,品牌建设交流问题有哪些Made By 于子轩#xff0c;2025.2.2
不管是使用System.IO命名空间下的File类来创建快捷方式文件#xff0c;或是使用Windows Script Host对象创建快捷方式#xff0c;亦或是使用Shell32对象创建快捷方式#xff0c;都对用户很不友好#xff0c;今天小编为大家带来一种全新…Made By 于子轩2025.2.2
不管是使用System.IO命名空间下的File类来创建快捷方式文件或是使用Windows Script Host对象创建快捷方式亦或是使用Shell32对象创建快捷方式都对用户很不友好今天小编为大家带来一种全新的方式调用控制面板项.Cpl实现“新建快捷方式对话框” 别人的方法
在C#中可以使用WshShell对象来创建快捷方式。下面是一个简单的示例代码 csharp复制插入
using IWshRuntimeLibrary;public void CreateShortcut(string targetPath, string shortcutPath)
{WshShell shell new WshShell();IWshShortcut shortcut (IWshShortcut)shell.CreateShortcut(shortcutPath);shortcut.TargetPath targetPath;shortcut.Save();
}在这个示例中targetPath参数指定了目标文件或文件夹的路径shortcutPath参数指定了要创建的快捷方式的路径。首先我们创建一个WshShell对象然后使用其CreateShortcut方法创建一个快捷方式对象。接下来我们可以设置快捷方式对象的属性例如目标路径(TargetPath)、图标路径(IconLocation)等最后保存快捷方式(Save)。
别人的方法
在C#中可以使用System.IO命名空间下的FileSystem类来创建快捷方式。下面是一个示例代码 csharp复制插入
using System;
using System.IO;
using IWshRuntimeLibrary;namespace ShortcutDemo
{class Program{static void Main(string[] args){string targetPath C:\Path\To\Your\File.txt;string shortcutPath C:\Path\To\Your\Shortcut.lnk;CreateShortcut(targetPath, shortcutPath);Console.WriteLine(Shortcut created successfully!);}static void CreateShortcut(string targetPath, string shortcutPath){WshShell shell new WshShell();IWshShortcut shortcut (IWshShortcut)shell.CreateShortcut(shortcutPath);shortcut.TargetPath targetPath;shortcut.Save();}}
}上述代码中CreateShortcut方法接受两个参数分别是目标文件路径和快捷方式路径。在CreateShortcut方法中我们首先创建了一个WshShell对象然后使用CreateShortcut方法创建了一个IWshShortcut对象。接着我们设置shortcut.TargetPath为目标文件路径并保存快捷方式。 我的方法 先附图 CPL文件与DLL文件类似都具有“导出函数”在appwiz.cpl用于管理已安装的程序和功能。通过运行appwiz.cpl用户可以打开“程序和功能”窗口该窗口允许用户查看、更改或卸载已安装的程序。中有一个函数“NewLinkHereW”可以实现我们的要求 [DllImport(appwiz.cpl, SetLastError true, CharSet CharSet.Unicode)] public static extern int NewLinkHereW( IntPtr hwndCpl, int msg, string lParam1, string lParam2 );
这个函数的签名是CPL文件标准方式来写的一般我们需要提供四个参数。
应用
要想成功发起该对画框创建一个文件函数将删除这个文件并在这个文件原位置上建立快捷方式
NewLinkHereW(0,0,创建的文件地址,null);