@@ -132,16 +132,16 @@ private static IEnumerable<HostsFileEntry> GetHostsFileEntries()
132132
133133 if ( result . Success )
134134 {
135- Log . Debug ( "GetHostsFileEntries - Line matched: " + line ) ;
135+ Log . Debug ( $ "GetHostsFileEntries - Line matched: { line } " ) ;
136136
137137 var entry = new HostsFileEntry
138- {
139- IsEnabled = ! result . Groups [ 1 ] . Value . Equals ( "#" ) ,
140- IPAddress = result . Groups [ 2 ] . Value ,
141- Hostname = result . Groups [ 3 ] . Value . Replace ( @"\s" , "" ) . Trim ( ) ,
142- Comment = result . Groups [ 4 ] . Value . TrimStart ( '#' , ' ' ) ,
143- Line = line
144- } ;
138+ (
139+ ! result . Groups [ 1 ] . Value . Equals ( "#" ) ,
140+ result . Groups [ 2 ] . Value ,
141+ result . Groups [ 3 ] . Value . Replace ( @"\s" , "" ) . Trim ( ) ,
142+ result . Groups [ 4 ] . Value . TrimStart ( '#' , ' ' ) ,
143+ line
144+ ) ;
145145
146146 // Skip example entries
147147 if ( ! entry . IsEnabled )
@@ -157,7 +157,7 @@ private static IEnumerable<HostsFileEntry> GetHostsFileEntries()
157157 }
158158 else
159159 {
160- Log . Debug ( "GetHostsFileEntries - Line not matched: " + line ) ;
160+ Log . Debug ( $ "GetHostsFileEntries - Line not matched: { line } " ) ;
161161 }
162162 }
163163
@@ -201,31 +201,34 @@ private static HostsFileEntryModifyResult EnableEntry(HostsFileEntry entry)
201201 return HostsFileEntryModifyResult . ReadError ;
202202 }
203203
204- bool entryFound = false ;
204+ var entryFound = false ;
205205
206206 for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
207207 {
208- if ( hostsFileLines [ i ] == entry . Line )
209- {
210- entryFound = true ;
208+ if ( hostsFileLines [ i ] != entry . Line )
209+ continue ;
211210
212- hostsFileLines . RemoveAt ( i ) ;
213- hostsFileLines . Insert ( i , CreateEntryLine ( new HostsFileEntry
214- {
215- IsEnabled = true ,
216- IPAddress = entry . IPAddress ,
217- Hostname = entry . Hostname ,
218- Comment = entry . Comment ,
219- Line = entry . Line
220- } ) ) ;
221-
222- break ;
223- }
211+ entryFound = true ;
212+
213+ Log . Debug ( $ "EnableEntry - Found entry: { hostsFileLines [ i ] } ") ;
214+ hostsFileLines . RemoveAt ( i ) ;
215+
216+ var newEntry = new HostsFileEntry (
217+ true ,
218+ entry . IPAddress ,
219+ entry . Hostname ,
220+ entry . Comment
221+ ) ;
222+
223+ Log . Debug ( $ "EnableEntry - Enabling entry: { newEntry . Line } ") ;
224+ hostsFileLines . Insert ( i , newEntry . Line ) ;
225+
226+ break ;
224227 }
225228
226229 if ( ! entryFound )
227230 {
228- Log . Warn ( $ "EnableEntry - Entry not found in hosts file: { entry . Line } ") ;
231+ Log . Warn ( $ "EnableEntry - Entry not found in hosts file: { entry } ") ;
229232
230233 return HostsFileEntryModifyResult . NotFound ;
231234 }
@@ -281,26 +284,29 @@ private static HostsFileEntryModifyResult DisableEntry(HostsFileEntry entry)
281284 return HostsFileEntryModifyResult . ReadError ;
282285 }
283286
284- bool entryFound = false ;
287+ var entryFound = false ;
285288
286289 for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
287290 {
288- if ( hostsFileLines [ i ] == entry . Line )
289- {
290- entryFound = true ;
291+ if ( hostsFileLines [ i ] != entry . Line )
292+ continue ;
291293
292- hostsFileLines . RemoveAt ( i ) ;
293- hostsFileLines . Insert ( i , CreateEntryLine ( new HostsFileEntry
294- {
295- IsEnabled = false ,
296- IPAddress = entry . IPAddress ,
297- Hostname = entry . Hostname ,
298- Comment = entry . Comment ,
299- Line = entry . Line
300- } ) ) ;
301-
302- break ;
303- }
294+ entryFound = true ;
295+
296+ Log . Debug ( $ "DisableEntry - Found entry: { hostsFileLines [ i ] } ") ;
297+ hostsFileLines . RemoveAt ( i ) ;
298+
299+ var newEntry = new HostsFileEntry (
300+ false ,
301+ entry . IPAddress ,
302+ entry . Hostname ,
303+ entry . Comment
304+ ) ;
305+
306+ Log . Debug ( $ "DisableEntry - Disabling entry: { newEntry . Line } ") ;
307+ hostsFileLines . Insert ( i , newEntry . Line ) ;
308+
309+ break ;
304310 }
305311
306312 if ( ! entryFound )
@@ -360,7 +366,8 @@ private static HostsFileEntryModifyResult AddEntry(HostsFileEntry entry)
360366 return HostsFileEntryModifyResult . ReadError ;
361367 }
362368
363- hostsFileLines . Add ( CreateEntryLine ( entry ) ) ;
369+ Log . Debug ( $ "AddEntry - Adding entry: { entry . Line } ") ;
370+ hostsFileLines . Add ( entry . Line ) ;
364371
365372 try
366373 {
@@ -415,19 +422,22 @@ private static HostsFileEntryModifyResult EditEntry(HostsFileEntry entry, HostsF
415422 return HostsFileEntryModifyResult . ReadError ;
416423 }
417424
418- bool entryFound = false ;
425+ var entryFound = false ;
419426
420427 for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
421428 {
422- if ( hostsFileLines [ i ] == entry . Line )
423- {
424- entryFound = true ;
429+ if ( hostsFileLines [ i ] != entry . Line )
430+ continue ;
425431
426- hostsFileLines . RemoveAt ( i ) ;
427- hostsFileLines . Insert ( i , CreateEntryLine ( newEntry ) ) ;
432+ entryFound = true ;
428433
429- break ;
430- }
434+ Log . Debug ( $ "EditEntry - Found entry: { hostsFileLines [ i ] } ") ;
435+ hostsFileLines . RemoveAt ( i ) ;
436+
437+ Log . Debug ( $ "EditEntry - Editing entry: { newEntry . Line } ") ;
438+ hostsFileLines . Insert ( i , newEntry . Line ) ;
439+
440+ break ;
431441 }
432442
433443 if ( ! entryFound )
@@ -487,18 +497,19 @@ private static HostsFileEntryModifyResult DeleteEntry(HostsFileEntry entry)
487497 return HostsFileEntryModifyResult . ReadError ;
488498 }
489499
490- bool entryFound = false ;
500+ var entryFound = false ;
491501
492502 for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
493503 {
494- if ( hostsFileLines [ i ] == entry . Line )
495- {
496- entryFound = true ;
504+ if ( hostsFileLines [ i ] != entry . Line )
505+ continue ;
497506
498- hostsFileLines . RemoveAt ( i ) ;
507+ entryFound = true ;
499508
500- break ;
501- }
509+ Log . Debug ( $ "DeleteEntry - Found entry: { hostsFileLines [ i ] } ") ;
510+ hostsFileLines . RemoveAt ( i ) ;
511+
512+ break ;
502513 }
503514
504515 if ( ! entryFound )
@@ -521,25 +532,6 @@ private static HostsFileEntryModifyResult DeleteEntry(HostsFileEntry entry)
521532 return HostsFileEntryModifyResult . Success ;
522533 }
523534
524- /// <summary>
525- /// Create a line for the hosts file entry.
526- /// </summary>
527- /// <param name="entry">Entry to create the line for.</param>
528- /// <returns>Line for the hosts file entry.</returns>
529- private static string CreateEntryLine ( HostsFileEntry entry )
530- {
531- var line = entry . IsEnabled ? "" : "# " ;
532-
533- line += $ "{ entry . IPAddress } { entry . Hostname } ";
534-
535- if ( ! string . IsNullOrWhiteSpace ( entry . Comment ) )
536- {
537- line += $ " # { entry . Comment } ";
538- }
539-
540- return line . Trim ( ) ;
541- }
542-
543535 /// <summary>
544536 /// Create a "daily" backup of the hosts file (before making a change).
545537 /// </summary>
0 commit comments