1+ #region
2+
13using System ;
2- using System . Diagnostics ;
3- using System . Diagnostics . CodeAnalysis ;
44using System . IO ;
55using System . Linq ;
6- using System . Reflection ;
7- using System . Security . Permissions ;
8- using Microsoft . Win32 ;
96using Neocmd ;
10- using SmartImage . Engines ;
117using SmartImage . Searching ;
128using SmartImage . Utilities ;
139
10+ #endregion
11+
1412namespace SmartImage
1513{
1614 public static class Config
@@ -37,7 +35,7 @@ public static class Config
3735
3836 internal static string AppFolder {
3937 get {
40- var folder = Path . GetDirectoryName ( Location ) ;
38+ string ? folder = Path . GetDirectoryName ( Location ) ;
4139
4240
4341 return folder ;
@@ -48,23 +46,23 @@ internal static string AppFolder {
4846 internal static bool IsExeInAppFolder => File . Exists ( Path . Combine ( AppFolder , NAME_EXE ) ) ;
4947
5048 /// <summary>
51- /// Null if executable is not in path.
49+ /// Null if executable is not in path.
5250 /// </summary>
5351 internal static string Location => FindExecutableLocation ( NAME_EXE ) ;
5452
5553 internal static bool IsContextMenuAdded {
5654 get {
57- var cmdStr = string . Format ( @"reg query {0}" , REG_SHELL_CMD ) ;
58- var cmd = Cli . Shell ( cmdStr , true ) ;
55+ string cmdStr = String . Format ( @"reg query {0}" , REG_SHELL_CMD ) ;
56+ var cmd = Cli . Shell ( cmdStr , true ) ;
5957
60- var stdOut = Cli . ReadAllLines ( cmd . StandardOutput ) ;
58+ string [ ] stdOut = Cli . ReadAllLines ( cmd . StandardOutput ) ;
6159
6260 // todo
6361 if ( stdOut . Any ( s => s . Contains ( NAME ) ) ) {
6462 return true ;
6563 }
6664
67- var stdErr = Cli . ReadAllLines ( cmd . StandardError ) ;
65+ string [ ] stdErr = Cli . ReadAllLines ( cmd . StandardError ) ;
6866
6967 if ( stdErr . Any ( s => s . Contains ( "ERROR" ) ) ) {
7068 return false ;
@@ -75,13 +73,7 @@ internal static bool IsContextMenuAdded {
7573 }
7674 }
7775
78- internal static bool IsAppFolderInPath {
79- get {
80- string dir = Win32 . GetEnvironmentPath ( ) ? . Split ( ';' ) . FirstOrDefault ( s => s == AppFolder ) ;
81-
82- return ! String . IsNullOrWhiteSpace ( dir ) ;
83- }
84- }
76+ internal static bool IsAppFolderInPath => ExplorerSystem . IsFolderInPath ( AppFolder ) ;
8577
8678 private static RegistryConfig RegConfig { get ; } = new RegistryConfig ( REG_SUBKEY ) ;
8779
@@ -101,7 +93,7 @@ internal static AuthInfo ImgurAuth {
10193
10294 return new AuthInfo ( id ) ;
10395 }
104- set { RegConfig . Write ( REG_IMGUR_CLIENT_ID , value . Id ) ; }
96+ set => RegConfig . Write ( REG_IMGUR_CLIENT_ID , value . Id ) ;
10597 }
10698
10799 internal static AuthInfo SauceNaoAuth {
@@ -121,51 +113,39 @@ private static void RemoveFromContextMenu()
121113 string [ ] code =
122114 {
123115 "@echo off" ,
124- string . Format ( @"reg.exe delete {0} /f >nul" , REG_SHELL )
116+ String . Format ( @"reg.exe delete {0} /f >nul" , REG_SHELL )
125117 } ;
126118
127119 Cli . CreateRunBatchFile ( "rem_from_menu.bat" , code ) ;
128120 }
129121
130122 internal static void AddToPath ( )
131123 {
132- var oldValue = Win32 . GetEnvironmentPath ( ) ;
124+ string oldValue = ExplorerSystem . EnvironmentPath ;
133125
134- var appFolder = AppFolder ;
126+ string appFolder = AppFolder ;
135127
136128 if ( IsAppFolderInPath ) {
137129 CliOutput . WriteInfo ( "Executable is already in path: {0}" , Location ) ;
138130 return ;
139131 }
140132
141133
142- bool appFolderInPath = oldValue . Split ( ';' ) . Any ( p => p == appFolder ) ;
134+ bool appFolderInPath = oldValue . Split ( ExplorerSystem . PATH_DELIM ) . Any ( p => p == appFolder ) ;
143135
144136
145- var cd = Environment . CurrentDirectory ;
146- var exe = Path . Combine ( cd , NAME_EXE ) ;
137+ string cd = Environment . CurrentDirectory ;
138+ string exe = Path . Combine ( cd , NAME_EXE ) ;
147139
148140
149141 if ( appFolderInPath ) {
150142 CliOutput . WriteInfo ( "App folder already in path: {0}" , appFolder ) ;
151143 }
152144 else {
153- var newValue = oldValue + @";" + cd ;
154- Win32 . SetEnvironmentPath ( newValue ) ;
145+ string newValue = oldValue + ExplorerSystem . PATH_DELIM + cd ;
146+ ExplorerSystem . EnvironmentPath = newValue ;
155147 CliOutput . WriteInfo ( "Added {0} to path" , cd ) ;
156148 }
157-
158-
159- var dest = Path . Combine ( appFolder , NAME_EXE ) ;
160-
161-
162- // todo: fix
163- //Common.TryMove(exe, dest);
164- // Can't reload environment variables immediately
165- //Console.WriteLine("Global alloc: {0}", Alloc.Count);
166- //Environment.Exit(0);
167-
168- return ;
169149 }
170150
171151 internal static void Info ( )
@@ -196,10 +176,10 @@ internal static void Info()
196176
197177 internal static void AddToContextMenu ( )
198178 {
199- var fullPath = Location ;
179+ string fullPath = Location ;
200180
201181 if ( ! IsExeInAppFolder ) {
202- var v = CliOutput . ReadConfirm ( "Could not find exe in system path. Add now?" ) ;
182+ bool v = CliOutput . ReadConfirm ( "Could not find exe in system path. Add now?" ) ;
203183
204184 if ( v ) {
205185 AddToPath ( ) ;
@@ -216,9 +196,7 @@ internal static void AddToContextMenu()
216196 string [ ] commandCode =
217197 {
218198 "@echo off" ,
219- //String.Format("SET \"SMARTIMAGE={0}\"", fullPath),
220- //"SET COMMAND=%SMARTIMAGE% \"\"%%1\"\"",
221- string . Format ( "reg.exe add {0} /ve /d \" {1} \" \" %%1\" \" \" /f >nul" , REG_SHELL_CMD , fullPath )
199+ String . Format ( "reg.exe add {0} /ve /d \" {1} \" \" %%1\" \" \" /f >nul" , REG_SHELL_CMD , fullPath )
222200 } ;
223201
224202 Cli . CreateRunBatchFile ( "add_to_menu.bat" , commandCode ) ;
@@ -228,24 +206,13 @@ internal static void AddToContextMenu()
228206 string [ ] iconCode =
229207 {
230208 "@echo off" ,
231- //String.Format("SET \"SMARTIMAGE={0}\"", fullPath),
232- //"SET ICO=%SMARTIMAGE%",
233209 String . Format ( "reg.exe add {0} /v Icon /d \" {1}\" /f >nul" , REG_SHELL , fullPath ) ,
234210 } ;
235211
236212 Cli . CreateRunBatchFile ( "add_icon_to_menu.bat" , iconCode ) ;
237213 }
238214
239- internal static void RemoveFromPath ( )
240- {
241- var oldValue = Win32 . GetEnvironmentPath ( ) ;
242-
243-
244- var newValue = oldValue . Replace ( ";" + AppFolder , String . Empty ) ;
245-
246-
247- Win32 . SetEnvironmentPath ( newValue ) ;
248- }
215+ internal static void RemoveFromPath ( ) => ExplorerSystem . RemoveFromPath ( AppFolder ) ;
249216
250217 internal static void Reset ( bool all = false )
251218 {
@@ -267,47 +234,23 @@ internal static void Reset(bool all = false)
267234 Info ( ) ;
268235 }
269236
270- internal static void Check ( )
271- {
272- //var files = AppFolder.GetFiles("*.exe").Any(f => f.Name == NAME_EXE);
273-
274- //CliOutput.WriteInfo("{0}", files);
275-
276- //var l = new FileInfo(Location);
277- //bool exeInAppFolder = l.DirectoryName == AppFolder.Name;
278- }
279-
280- private static string GetPath ( string exe )
281- {
282- string dir = Win32 . GetEnvironmentPath ( ) . Split ( ';' )
283- . FirstOrDefault ( s => File . Exists ( Path . Combine ( s , exe ) ) ) ;
284-
285- if ( ! String . IsNullOrWhiteSpace ( dir ) ) {
286- return Path . Combine ( dir , exe ) ;
287- }
288-
289- return null ;
290- }
291-
292- internal static string FindExecutableLocation ( string exe )
237+ private static string FindExecutableLocation ( string exe )
293238 {
294- var path = GetPath ( exe ) ;
239+ string path = ExplorerSystem . FindExectableInPath ( exe ) ;
295240
296241 if ( path == null ) {
297- var cd = Environment . CurrentDirectory ;
298- var cdExe = Path . Combine ( cd , exe ) ;
299- var inCd = File . Exists ( cdExe ) ;
242+ string cd = Environment . CurrentDirectory ;
243+ string cdExe = Path . Combine ( cd , exe ) ;
244+ bool inCd = File . Exists ( cdExe ) ;
300245
301246 if ( inCd ) {
302247 return cdExe ;
303248 }
304249
305- else {
306- var appFolderExe = Path . Combine ( AppFolder , exe ) ;
307- var inAppFolder = File . Exists ( appFolderExe ) ;
308- if ( inAppFolder ) {
309- return appFolderExe ;
310- }
250+ string appFolderExe = Path . Combine ( AppFolder , exe ) ;
251+ bool inAppFolder = File . Exists ( appFolderExe ) ;
252+ if ( inAppFolder ) {
253+ return appFolderExe ;
311254 }
312255 }
313256
0 commit comments