66using System . Globalization ;
77using System . IO ;
88using System . Linq ;
9+ using System . Runtime . Versioning ;
910using System . Threading ;
1011using Kantan . Cli ;
1112using Novus . Utilities ;
1920
2021namespace SmartImage . Core
2122{
22-
23-
2423 /// <summary>
2524 /// Program OS integrations
2625 /// </summary>
@@ -39,64 +38,66 @@ public static bool HandleContextMenu(bool option)
3938 /*
4039 * New context menu
4140 */
41+ if ( OperatingSystem . IsWindows ( ) ) {
42+ switch ( option ) {
43+ case true :
4244
43- switch ( option ) {
44- case true :
45+ RegistryKey regMenu = null ;
46+ RegistryKey regCmd = null ;
4547
46- RegistryKey regMenu = null ;
47- RegistryKey regCmd = null ;
48+ string fullPath = AppInfo . ExeLocation ;
4849
49- string fullPath = AppInfo . ExeLocation ;
50+ try {
51+ regMenu = Registry . CurrentUser . CreateSubKey ( REG_SHELL ) ;
52+ regMenu ? . SetValue ( String . Empty , AppInfo . NAME ) ;
53+ regMenu ? . SetValue ( "Icon" , $ "\" { fullPath } \" ") ;
5054
51- try {
52- regMenu = Registry . CurrentUser . CreateSubKey ( REG_SHELL ) ;
53- regMenu ? . SetValue ( String . Empty , AppInfo . NAME ) ;
54- regMenu ? . SetValue ( "Icon" , $ "\" { fullPath } \" ") ;
55+ regCmd = Registry . CurrentUser . CreateSubKey ( REG_SHELL_CMD ) ;
56+ regCmd ? . SetValue ( String . Empty , $ "\" { fullPath } \" \" %1\" ") ;
57+ }
58+ catch ( Exception ex ) {
59+ Trace . WriteLine ( $ "{ ex . Message } ") ;
60+ ConsoleManager . WaitForInput ( ) ;
61+ return false ;
62+ }
63+ finally {
64+ regMenu ? . Close ( ) ;
65+ regCmd ? . Close ( ) ;
66+ }
5567
56- regCmd = Registry . CurrentUser . CreateSubKey ( REG_SHELL_CMD ) ;
57- regCmd ? . SetValue ( String . Empty , $ "\" { fullPath } \" \" %1\" ") ;
58- }
59- catch ( Exception ex ) {
60- Trace . WriteLine ( $ "{ ex . Message } ") ;
61- ConsoleManager . WaitForInput ( ) ;
62- return false ;
63- }
64- finally {
65- regMenu ? . Close ( ) ;
66- regCmd ? . Close ( ) ;
67- }
68+ break ;
69+ case false :
6870
69- break ;
70- case false :
71+ try {
72+ var reg = Registry . CurrentUser . OpenSubKey ( REG_SHELL_CMD ) ;
7173
72- try {
73- var reg = Registry . CurrentUser . OpenSubKey ( REG_SHELL_CMD ) ;
74+ if ( reg != null ) {
75+ reg . Close ( ) ;
76+ Registry . CurrentUser . DeleteSubKey ( REG_SHELL_CMD ) ;
77+ }
7478
75- if ( reg != null ) {
76- reg . Close ( ) ;
77- Registry . CurrentUser . DeleteSubKey ( REG_SHELL_CMD ) ;
78- }
79+ reg = Registry . CurrentUser . OpenSubKey ( REG_SHELL ) ;
7980
80- reg = Registry . CurrentUser . OpenSubKey ( REG_SHELL ) ;
81+ if ( reg != null ) {
82+ reg . Close ( ) ;
83+ Registry . CurrentUser . DeleteSubKey ( REG_SHELL ) ;
84+ }
85+ }
86+ catch ( Exception ex ) {
87+ Trace . WriteLine ( $ "{ ex . Message } ", C_ERROR ) ;
8188
82- if ( reg != null ) {
83- reg . Close ( ) ;
84- Registry . CurrentUser . DeleteSubKey ( REG_SHELL ) ;
89+ return false ;
8590 }
86- }
87- catch ( Exception ex ) {
88- Trace . WriteLine ( $ "{ ex . Message } ", C_ERROR ) ;
8991
90- return false ;
91- }
92+ break ;
93+
94+ }
9295
93- break ;
94- default :
95- throw new ArgumentOutOfRangeException ( nameof ( option ) , option , null ) ;
96- }
96+
9797
98+ }
9899
99- return true ;
100+ return false ;
100101
101102 }
102103
@@ -140,12 +141,16 @@ public static void ResetIntegrations()
140141 // Computer\HKEY_CLASSES_ROOT\*\shell\SmartImage
141142
142143 if ( IsContextMenuAdded ) {
143- HandleContextMenu ( false ) ;
144+ if ( OperatingSystem . IsWindows ( ) ) {
145+ HandleContextMenu ( false ) ;
146+
147+ }
144148 }
145149
146150 Trace . WriteLine ( "Reset config" ) ;
147151 }
148152
153+ [ SupportedOSPlatform ( "windows" ) ]
149154 [ DoesNotReturn ]
150155 public static void Uninstall ( )
151156 {
@@ -186,9 +191,14 @@ public static bool IsContextMenuAdded
186191 {
187192 get
188193 {
189- var reg = Registry . CurrentUser . OpenSubKey ( REG_SHELL_CMD ) ;
190194
191- return reg != null ;
195+ if ( OperatingSystem . IsWindows ( ) ) {
196+ var reg = Registry . CurrentUser . OpenSubKey ( REG_SHELL_CMD ) ;
197+ return reg != null ;
198+
199+ }
200+
201+ return false ;
192202 }
193203 }
194204 }
0 commit comments