@@ -164,27 +164,52 @@ private static IEnumerable<HostsFileEntry> GetHostsFileEntries()
164164 return entries ;
165165 }
166166
167- public static Task < bool > EnableEntryAsync ( HostsFileEntry entry )
167+ /// <summary>
168+ /// Enable a hosts file entry asynchronously.
169+ /// </summary>
170+ /// <param name="entry">Entry to enable.</param>
171+ /// <returns><see cref="HostsFileEntryModifyResult.Success"/> if the entry was enabled successfully, otherwise an error result.</returns>
172+ public static Task < HostsFileEntryModifyResult > EnableEntryAsync ( HostsFileEntry entry )
168173 {
169174 return Task . Run ( ( ) => EnableEntry ( entry ) ) ;
170175 }
171176
172- private static bool EnableEntry ( HostsFileEntry entry )
177+ /// <summary>
178+ /// Enable a hosts file entry.
179+ /// </summary>
180+ /// <param name="entry">Entry to enable.</param>
181+ /// <returns><see cref="HostsFileEntryModifyResult.Success"/> if the entry was enabled successfully, otherwise an error result.</returns>
182+ private static HostsFileEntryModifyResult EnableEntry ( HostsFileEntry entry )
173183 {
174184 // Create a backup of the hosts file before making changes
175185 if ( CreateBackup ( ) == false )
176186 {
177187 Log . Error ( "EnableEntry - Failed to create backup before enabling entry." ) ;
178- return false ;
188+ return HostsFileEntryModifyResult . BackupError ;
179189 }
180190
181191 // Replace the entry in the hosts file
182192 var hostsFileLines = File . ReadAllLines ( HostsFilePath ) . ToList ( ) ;
183193
194+ bool entryFound = false ;
195+
184196 for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
185197 {
186198 if ( hostsFileLines [ i ] == entry . Line )
199+ {
200+ entryFound = true ;
201+
187202 hostsFileLines [ i ] = entry . Line . TrimStart ( '#' , ' ' ) ;
203+
204+ break ;
205+ }
206+ }
207+
208+ if ( ! entryFound )
209+ {
210+ Log . Warn ( $ "EnableEntry - Entry not found in hosts file: { entry . Line } ") ;
211+
212+ return HostsFileEntryModifyResult . NotFound ;
188213 }
189214
190215 try
@@ -195,34 +220,57 @@ private static bool EnableEntry(HostsFileEntry entry)
195220 catch ( Exception ex )
196221 {
197222 Log . Error ( $ "EnableEntry - Failed to write changes to hosts file: { HostsFilePath } ", ex ) ;
198-
199- return false ;
223+ return HostsFileEntryModifyResult . WriteError ;
200224 }
201225
202- return true ;
226+ return HostsFileEntryModifyResult . Success ;
203227 }
204228
205- public static Task < bool > DisableEntryAsync ( HostsFileEntry entry )
229+ /// <summary>
230+ /// Disable a hosts file entry asynchronously.
231+ /// </summary>
232+ /// <param name="entry">Entry to disable.</param>
233+ /// <returns><see cref="HostsFileEntryModifyResult.Success"/> if the entry was enabled successfully, otherwise an error result.</returns>
234+ public static Task < HostsFileEntryModifyResult > DisableEntryAsync ( HostsFileEntry entry )
206235 {
207236 return Task . Run ( ( ) => DisableEntry ( entry ) ) ;
208237 }
209238
210- private static bool DisableEntry ( HostsFileEntry entry )
239+ /// <summary>
240+ /// Disable a hosts file entry.
241+ /// </summary>
242+ /// <param name="entry">Entry to disable.</param>
243+ /// <returns><see cref="HostsFileEntryModifyResult.Success"/> if the entry was enabled successfully, otherwise an error result.</returns>
244+ private static HostsFileEntryModifyResult DisableEntry ( HostsFileEntry entry )
211245 {
212246 // Create a backup of the hosts file before making changes
213247 if ( CreateBackup ( ) == false )
214248 {
215249 Log . Error ( "DisableEntry - Failed to create backup before disabling entry." ) ;
216- return false ;
250+ return HostsFileEntryModifyResult . BackupError ;
217251 }
218252
219253 // Replace the entry in the hosts file
220254 var hostsFileLines = File . ReadAllLines ( HostsFilePath ) . ToList ( ) ;
221255
256+ bool entryFound = false ;
257+
222258 for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
223259 {
224260 if ( hostsFileLines [ i ] == entry . Line )
261+ {
262+ entryFound = true ;
263+
225264 hostsFileLines [ i ] = "# " + entry . Line ;
265+
266+ break ;
267+ }
268+ }
269+
270+ if ( ! entryFound )
271+ {
272+ Log . Warn ( $ "DisableEntry - Entry not found in hosts file: { entry . Line } ") ;
273+ return HostsFileEntryModifyResult . NotFound ;
226274 }
227275
228276 try
@@ -233,15 +281,75 @@ private static bool DisableEntry(HostsFileEntry entry)
233281 catch ( Exception ex )
234282 {
235283 Log . Error ( $ "DisableEntry - Failed to write changes to hosts file: { HostsFilePath } ", ex ) ;
284+ return HostsFileEntryModifyResult . WriteError ;
285+ }
236286
237- return false ;
287+ return HostsFileEntryModifyResult . Success ;
288+ }
289+
290+ /// <summary>
291+ /// Delete a hosts file entry asynchronously.
292+ /// </summary>
293+ /// <param name="entry">Entry to delete.</param>"/>
294+ /// <returns><see cref="HostsFileEntryModifyResult.Success"/> if the entry was enabled successfully, otherwise an error result.</returns>s
295+ public static Task < HostsFileEntryModifyResult > DeleteEntryAsync ( HostsFileEntry entry )
296+ {
297+ return Task . Run ( ( ) => DeleteEntry ( entry ) ) ;
298+ }
299+
300+ /// <summary>
301+ /// Delete a hosts file entry.
302+ /// </summary>
303+ /// <param name="entry">Entry to delete.</param>"/>
304+ /// <returns><see cref="HostsFileEntryModifyResult.Success"/> if the entry was enabled successfully, otherwise an error result.</returns>
305+ private static HostsFileEntryModifyResult DeleteEntry ( HostsFileEntry entry )
306+ {
307+ // Create a backup of the hosts file before making changes
308+ if ( CreateBackup ( ) == false )
309+ {
310+ Log . Error ( "DeleteEntry - Failed to create backup before deleting entry." ) ;
311+ return HostsFileEntryModifyResult . BackupError ;
238312 }
239313
240- return true ;
314+ // Remove the entry from the hosts file
315+ var hostsFileLines = File . ReadAllLines ( HostsFilePath ) . ToList ( ) ;
316+
317+ bool entryFound = false ;
318+
319+ for ( var i = 0 ; i < hostsFileLines . Count ; i ++ )
320+ {
321+ if ( hostsFileLines [ i ] == entry . Line )
322+ {
323+ entryFound = true ;
324+
325+ hostsFileLines . RemoveAt ( i ) ;
326+
327+ break ;
328+ }
329+ }
330+
331+ if ( ! entryFound )
332+ {
333+ Log . Warn ( $ "DeleteEntry - Entry not found in hosts file: { entry . Line } ") ;
334+ return HostsFileEntryModifyResult . NotFound ;
335+ }
336+
337+ try
338+ {
339+ Log . Debug ( $ "DeleteEntry - Writing changes to hosts file: { HostsFilePath } ") ;
340+ File . WriteAllLines ( HostsFilePath , hostsFileLines ) ;
341+ }
342+ catch ( Exception ex )
343+ {
344+ Log . Error ( $ "DeleteEntry - Failed to write changes to hosts file: { HostsFilePath } ", ex ) ;
345+ return HostsFileEntryModifyResult . WriteError ;
346+ }
347+ OnHostsFileChanged ( ) ;
348+ return HostsFileEntryModifyResult . Success ;
241349 }
242350
243351 /// <summary>
244- /// Create a daily backup of the hosts file (before making a change).
352+ /// Create a " daily" backup of the hosts file (before making a change).
245353 /// </summary>
246354 private static bool CreateBackup ( )
247355 {
0 commit comments