-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
224 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using SWMI = Skylark.Wing.Manage.Internal; | ||
using SWNM = Skylark.Wing.Native.Methods; | ||
|
||
namespace Skylark.Wing.Helper | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static class ShortcutBasic | ||
{ | ||
/// <summary> | ||
/// Create shortcut in current path. | ||
/// </summary> | ||
/// <param name="linkFileName">shortcut name(include .lnk extension.)</param> | ||
/// <param name="targetPath">target path</param> | ||
/// <param name="workingDirectory">working path</param> | ||
/// <param name="arguments">arguments</param> | ||
/// <param name="hotkey">hot key(ex: Ctrl+Shift+Alt+A)</param> | ||
/// <param name="shortcutWindowStyle">window style</param> | ||
/// <param name="description">shortcut description</param> | ||
/// <param name="iconNumber">icon index(start of 0)</param> | ||
/// <returns></returns> | ||
/// <exception cref="FileNotFoundException"></exception> | ||
public static void Create(string linkFileName, string targetPath, string workingDirectory = "", string arguments = "", string hotkey = "", SWNM.ShortcutWindowStyles shortcutWindowStyle = SWNM.ShortcutWindowStyles.WshNormalFocus, string description = "", int iconNumber = 0) | ||
{ | ||
if (linkFileName.EndsWith(SWMI.DEFAULT_SHORTCUT_EXTENSION) == false) | ||
{ | ||
linkFileName = string.Format("{0}{1}", linkFileName, SWMI.DEFAULT_SHORTCUT_EXTENSION); | ||
} | ||
|
||
if (File.Exists(targetPath) == false) | ||
{ | ||
throw new FileNotFoundException(targetPath); | ||
} | ||
|
||
if (workingDirectory == string.Empty) | ||
{ | ||
workingDirectory = Path.GetDirectoryName(targetPath); | ||
} | ||
|
||
string iconLocation = string.Format("{0},{1}", targetPath, iconNumber); | ||
|
||
#if NET6_0_OR_GREATER | ||
Type shellType = SWMI.M_TYPE; | ||
dynamic shell = SWMI.M_SHELL; | ||
dynamic shortcut = shell.CreateShortcut(linkFileName); | ||
|
||
shortcut.TargetPath = targetPath; | ||
shortcut.WorkingDirectory = workingDirectory; | ||
shortcut.Arguments = arguments; | ||
shortcut.Hotkey = hotkey; | ||
shortcut.WindowStyle = shortcutWindowStyle; | ||
shortcut.Description = description; | ||
shortcut.IconLocation = iconLocation; | ||
|
||
shortcut.Save(); | ||
#else | ||
Type shellType = SWMI.M_TYPE; | ||
object shell = SWMI.M_SHELL; | ||
object shortcut = shellType.InvokeMethod("CreateShortcut", shell, linkFileName); | ||
Type shortcutType = shortcut.GetType(); | ||
|
||
shortcutType.InvokeSetMember("TargetPath", shortcut, targetPath); | ||
shortcutType.InvokeSetMember("WorkingDirectory", shortcut, workingDirectory); | ||
shortcutType.InvokeSetMember("Arguments", shortcut, arguments); | ||
shortcutType.InvokeSetMember("Hotkey", shortcut, hotkey); | ||
shortcutType.InvokeSetMember("WindowStyle", shortcut, shortcutWindowStyle); | ||
shortcutType.InvokeSetMember("Description", shortcut, description); | ||
shortcutType.InvokeSetMember("IconLocation", shortcut, iconLocation); | ||
|
||
shortcutType.InvokeMethod("Save", shortcut); | ||
#endif | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="type"></param> | ||
/// <param name="methodName"></param> | ||
/// <param name="targetInstance"></param> | ||
/// <param name="arguments"></param> | ||
/// <returns></returns> | ||
private static object InvokeSetMember(this Type type, string methodName, object targetInstance, params object[] arguments) | ||
{ | ||
return type.InvokeMember(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null, targetInstance, arguments); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="type"></param> | ||
/// <param name="methodName"></param> | ||
/// <param name="targetInstance"></param> | ||
/// <param name="arguments"></param> | ||
/// <returns></returns> | ||
private static object InvokeMethod(this Type type, string methodName, object targetInstance, params object[] arguments) | ||
{ | ||
return type.InvokeMember(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, targetInstance, arguments); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using IWshRuntimeLibrary; | ||
using System.IO; | ||
using SWNM = Skylark.Wing.Native.Methods; | ||
|
||
namespace Skylark.Wing.Helper | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static class ShortcutRuntime | ||
{ | ||
/// <summary> | ||
/// Creates a shortcut at a specified location with specified parameters. | ||
/// </summary> | ||
/// <param name="shortcutLocation">The location where the shortcut will be created (e.g., "C:\\SomeFolder").</param> | ||
/// <param name="shortcutName">The name of the shortcut (e.g., "Notepad.lnk").</param> | ||
/// <param name="description">The description for the shortcut.</param> | ||
/// <param name="hotkey">The hotkey for the shortcut (e.g., "Ctrl+Shift+N").</param> | ||
/// <param name="targetPath">The path to the executable file that the shortcut points to.</param> | ||
/// <param name="iconLocation">The location of the icon to be used for the shortcut.</param> | ||
/// <param name="workingDirectory">The working directory for the shortcut.</param> | ||
/// <param name="arguments">Arguments to be passed to the target application.</param> | ||
/// <param name="windowStyle">The window style for the shortcut.</param> | ||
public static void Create(string shortcutLocation, string shortcutName, string description, string hotkey, string targetPath, string iconLocation = null, string workingDirectory = null, string arguments = null, SWNM.WindowStyle windowStyle = SWNM.WindowStyle.Normal) | ||
{ | ||
WshShell shell = new(); | ||
string shortcutAddress = Path.Combine(shortcutLocation, shortcutName); | ||
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); | ||
|
||
shortcut.Description = description; | ||
shortcut.Hotkey = hotkey; | ||
shortcut.TargetPath = targetPath; | ||
|
||
if (iconLocation != null) | ||
{ | ||
shortcut.IconLocation = iconLocation; | ||
} | ||
|
||
if (workingDirectory != null) | ||
{ | ||
shortcut.WorkingDirectory = workingDirectory; | ||
} | ||
|
||
if (arguments != null) | ||
{ | ||
shortcut.Arguments = arguments; | ||
} | ||
|
||
shortcut.WindowStyle = (int)windowStyle; | ||
shortcut.Save(); | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters