@@ -132,16 +132,16 @@ private static IEnumerable<HostsFileEntry> GetHostsFileEntries()
132
132
133
133
if ( result . Success )
134
134
{
135
- Log . Debug ( "GetHostsFileEntries - Line matched: " + line ) ;
135
+ Log . Debug ( $ "GetHostsFileEntries - Line matched: { line } " ) ;
136
136
137
137
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
+ ) ;
145
145
146
146
// Skip example entries
147
147
if ( ! entry . IsEnabled )
@@ -157,7 +157,7 @@ private static IEnumerable<HostsFileEntry> GetHostsFileEntries()
157
157
}
158
158
else
159
159
{
160
- Log . Debug ( "GetHostsFileEntries - Line not matched: " + line ) ;
160
+ Log . Debug ( $ "GetHostsFileEntries - Line not matched: { line } " ) ;
161
161
}
162
162
}
163
163
@@ -201,31 +201,34 @@ private static HostsFileEntryModifyResult EnableEntry(HostsFileEntry entry)
201
201
return HostsFileEntryModifyResult . ReadError ;
202
202
}
203
203
204
- bool entryFound = false ;
204
+ var entryFound = false ;
205
205
206
206
for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
207
207
{
208
- if ( hostsFileLines [ i ] == entry . Line )
209
- {
210
- entryFound = true ;
208
+ if ( hostsFileLines [ i ] != entry . Line )
209
+ continue ;
211
210
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 ;
224
227
}
225
228
226
229
if ( ! entryFound )
227
230
{
228
- Log . Warn ( $ "EnableEntry - Entry not found in hosts file: { entry . Line } ") ;
231
+ Log . Warn ( $ "EnableEntry - Entry not found in hosts file: { entry } ") ;
229
232
230
233
return HostsFileEntryModifyResult . NotFound ;
231
234
}
@@ -281,26 +284,29 @@ private static HostsFileEntryModifyResult DisableEntry(HostsFileEntry entry)
281
284
return HostsFileEntryModifyResult . ReadError ;
282
285
}
283
286
284
- bool entryFound = false ;
287
+ var entryFound = false ;
285
288
286
289
for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
287
290
{
288
- if ( hostsFileLines [ i ] == entry . Line )
289
- {
290
- entryFound = true ;
291
+ if ( hostsFileLines [ i ] != entry . Line )
292
+ continue ;
291
293
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 ;
304
310
}
305
311
306
312
if ( ! entryFound )
@@ -360,7 +366,8 @@ private static HostsFileEntryModifyResult AddEntry(HostsFileEntry entry)
360
366
return HostsFileEntryModifyResult . ReadError ;
361
367
}
362
368
363
- hostsFileLines . Add ( CreateEntryLine ( entry ) ) ;
369
+ Log . Debug ( $ "AddEntry - Adding entry: { entry . Line } ") ;
370
+ hostsFileLines . Add ( entry . Line ) ;
364
371
365
372
try
366
373
{
@@ -415,19 +422,22 @@ private static HostsFileEntryModifyResult EditEntry(HostsFileEntry entry, HostsF
415
422
return HostsFileEntryModifyResult . ReadError ;
416
423
}
417
424
418
- bool entryFound = false ;
425
+ var entryFound = false ;
419
426
420
427
for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
421
428
{
422
- if ( hostsFileLines [ i ] == entry . Line )
423
- {
424
- entryFound = true ;
429
+ if ( hostsFileLines [ i ] != entry . Line )
430
+ continue ;
425
431
426
- hostsFileLines . RemoveAt ( i ) ;
427
- hostsFileLines . Insert ( i , CreateEntryLine ( newEntry ) ) ;
432
+ entryFound = true ;
428
433
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 ;
431
441
}
432
442
433
443
if ( ! entryFound )
@@ -487,18 +497,19 @@ private static HostsFileEntryModifyResult DeleteEntry(HostsFileEntry entry)
487
497
return HostsFileEntryModifyResult . ReadError ;
488
498
}
489
499
490
- bool entryFound = false ;
500
+ var entryFound = false ;
491
501
492
502
for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
493
503
{
494
- if ( hostsFileLines [ i ] == entry . Line )
495
- {
496
- entryFound = true ;
504
+ if ( hostsFileLines [ i ] != entry . Line )
505
+ continue ;
497
506
498
- hostsFileLines . RemoveAt ( i ) ;
507
+ entryFound = true ;
499
508
500
- break ;
501
- }
509
+ Log . Debug ( $ "DeleteEntry - Found entry: { hostsFileLines [ i ] } ") ;
510
+ hostsFileLines . RemoveAt ( i ) ;
511
+
512
+ break ;
502
513
}
503
514
504
515
if ( ! entryFound )
@@ -521,25 +532,6 @@ private static HostsFileEntryModifyResult DeleteEntry(HostsFileEntry entry)
521
532
return HostsFileEntryModifyResult . Success ;
522
533
}
523
534
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
-
543
535
/// <summary>
544
536
/// Create a "daily" backup of the hosts file (before making a change).
545
537
/// </summary>
0 commit comments