33using System . IO ;
44using System . Linq . Expressions ;
55using System . Reflection ;
6+ using System . Runtime . InteropServices . ComTypes ;
67using System . Text ;
78using System . Web . Script . Serialization ;
89
@@ -26,18 +27,22 @@ private static void AppendIndent(StringBuilder sb, int level, string indent)
2627 }
2728
2829 /// <summary>
29- /// Creates a directory exists. Ignores failures .
30+ /// Creates a shortcut (.lnk) at the specified path pointing to the target executable .
3031 /// </summary>
31- public static void CreateDirectory ( string path )
32+ /// <param name="targetExePath">The full path to the .exe file.</param>
33+ /// <param name="destinationLinkPath">The full path where the .lnk file will be saved.</param>
34+ /// <param name="description">The tooltip description for the shortcut.</param>
35+ public static void CreateShortcut ( string targetExePath , string destinationLinkPath , string description )
3236 {
33- try
34- {
35- if ( ! string . IsNullOrEmpty ( path ) && ! Directory . Exists ( path ) )
36- Directory . CreateDirectory ( path ) ;
37- }
38- catch
39- {
40- }
37+ var link = ( ShellInterop . IShellLink ) new ShellInterop . ShellLink ( ) ;
38+
39+ link . SetDescription ( description ) ;
40+ link . SetPath ( targetExePath ) ;
41+ link . SetWorkingDirectory ( Path . GetDirectoryName ( targetExePath ) ) ;
42+
43+ IPersistFile file = ( IPersistFile ) link ;
44+
45+ file . Save ( destinationLinkPath , false ) ;
4146 }
4247
4348 /// <summary>
@@ -144,25 +149,22 @@ private static string FormatJson(string json)
144149 }
145150
146151 /// <summary>
147- /// Gets the current application's assembly version.
152+ /// Returns the executable application's path
148153 /// </summary>
149- public static Version GetCurrentVersion ( )
154+ public static string GetExecutablePath ( )
150155 {
151156 try
152157 {
153- return ( Assembly . GetEntryAssembly ( ) ?? Assembly . GetExecutingAssembly ( ) ) . GetName ( ) . Version ?? new Version ( 0 , 0 , 0 , 0 ) ;
158+ var path = Process . GetCurrentProcess ( ) . MainModule . FileName ;
159+
160+ if ( ! string . IsNullOrEmpty ( path ) && File . Exists ( path ) )
161+ return path ;
154162 }
155- catch
163+ catch
156164 {
157- return new Version ( 0 , 0 , 0 , 0 ) ;
165+ // ignored
158166 }
159- }
160167
161- /// <summary>
162- /// Returns the executable path of the current process.
163- /// </summary>
164- public static string GetExecutablePath ( )
165- {
166168 try
167169 {
168170 var entry = Assembly . GetEntryAssembly ( ) ;
@@ -172,15 +174,24 @@ public static string GetExecutablePath()
172174 }
173175 catch
174176 {
177+ // ignored
175178 }
176179
180+ return Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , AppDomain . CurrentDomain . FriendlyName ) ;
181+ }
182+
183+ /// <summary>
184+ /// Gets the application's assembly version
185+ /// </summary>
186+ public static Version GetVersion ( )
187+ {
177188 try
178189 {
179- return Process . GetCurrentProcess ( ) . MainModule . FileName ;
190+ return ( Assembly . GetEntryAssembly ( ) ?? Assembly . GetExecutingAssembly ( ) ) . GetName ( ) . Version ?? new Version ( 0 , 0 , 0 , 0 ) ;
180191 }
181192 catch
182193 {
183- return Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , AppDomain . CurrentDomain . FriendlyName ) ;
194+ return new Version ( 0 , 0 , 0 , 0 ) ;
184195 }
185196 }
186197
0 commit comments