@@ -328,62 +328,68 @@ public async Task StopServiceAsync(string serviceId)
328328 }
329329
330330 public async Task DeleteServiceAsync ( string serviceId )
331- {
332- lock ( _lock )
333- {
334- if ( ! _services . ContainsKey ( serviceId ) ) throw new Exception ( "Service not found" ) ;
335-
336- // 清理 monitor
337- if ( _monitors . TryGetValue ( serviceId , out var monitor ) )
338331 {
339- monitor . Dispose ( ) ;
340- _monitors . Remove ( serviceId ) ;
341- }
342- }
332+ lock ( _lock )
333+ {
334+ if ( ! _services . ContainsKey ( serviceId ) ) throw new Exception ( "Service not found" ) ;
343335
344- await StopServiceAsync ( serviceId ) ;
336+ // 清理 monitor
337+ if ( _monitors . TryGetValue ( serviceId , out var monitor ) )
338+ {
339+ monitor . Dispose ( ) ;
340+ _monitors . Remove ( serviceId ) ;
341+ }
342+ }
345343
346- // Use P/Invoke to delete service
347- IntPtr scmHandle = ServiceUtils . OpenSCManager ( null , null , ServiceUtils . SC_MANAGER_CONNECT ) ;
348- if ( scmHandle == IntPtr . Zero )
349- throw new Exception ( $ "Failed to open SC Manager. Error: { Marshal . GetLastWin32Error ( ) } ") ;
344+ await StopServiceAsync ( serviceId ) ;
350345
351- try
352- {
353- // We need DELETE access
354- IntPtr serviceHandle = ServiceUtils . OpenService ( scmHandle , serviceId , ServiceUtils . DELETE ) ;
355- if ( serviceHandle == IntPtr . Zero )
356- throw new Exception ( $ "Failed to open service for deletion. Error: { Marshal . GetLastWin32Error ( ) } ") ;
346+ // Use P/Invoke to delete service
347+ IntPtr scmHandle = ServiceUtils . OpenSCManager ( null , null , ServiceUtils . SC_MANAGER_CONNECT ) ;
348+ if ( scmHandle == IntPtr . Zero )
349+ throw new Exception ( $ "Failed to open SC Manager. Error: { Marshal . GetLastWin32Error ( ) } ") ;
357350
358- try
359- {
360- if ( ! ServiceUtils . DeleteService ( serviceHandle ) )
361- throw new Exception ( $ "Failed to delete service. Error: { Marshal . GetLastWin32Error ( ) } ") ;
362- }
363- finally
364- {
365- ServiceUtils . CloseServiceHandle ( serviceHandle ) ;
366- }
367- }
368- finally
369- {
370- ServiceUtils . CloseServiceHandle ( scmHandle ) ;
371- }
372-
373- // Remove from managed services index
374- RemoveFromManagedServicesIndex ( serviceId ) ;
351+ try
352+ {
353+ // We need DELETE access
354+ IntPtr serviceHandle = ServiceUtils . OpenService ( scmHandle , serviceId , ServiceUtils . DELETE ) ;
355+ if ( serviceHandle == IntPtr . Zero )
356+ throw new Exception ( $ "Failed to open service for deletion. Error: { Marshal . GetLastWin32Error ( ) } ") ;
375357
376- lock ( _lock )
377- {
378- _services . Remove ( serviceId ) ;
379-
380- // 再次确保 monitor 已清理
381- if ( _monitors . ContainsKey ( serviceId ) )
382- {
383- _monitors . Remove ( serviceId ) ;
358+ try
359+ {
360+ if ( ! ServiceUtils . DeleteService ( serviceHandle ) )
361+ throw new Exception ( $ "Failed to delete service. Error: { Marshal . GetLastWin32Error ( ) } ") ;
362+ }
363+ finally
364+ {
365+ ServiceUtils . CloseServiceHandle ( serviceHandle ) ;
366+ }
367+ }
368+ finally
369+ {
370+ ServiceUtils . CloseServiceHandle ( scmHandle ) ;
371+ }
372+
373+ // Remove from managed services index
374+ RemoveFromManagedServicesIndex ( serviceId ) ;
375+
376+ // Clean up registry Parameters subkey
377+ try
378+ {
379+ using var key = Registry . LocalMachine . OpenSubKey ( $@ "SYSTEM\CurrentControlSet\Services\{ serviceId } ", writable : true ) ;
380+ key ? . DeleteSubKeyTree ( "Parameters" , throwOnMissingSubKey : false ) ;
381+ }
382+ catch ( Exception ex )
383+ {
384+ System . Diagnostics . Debug . WriteLine ( $ "Failed to delete Parameters registry key: { ex . Message } ") ;
385+ }
386+
387+ lock ( _lock )
388+ {
389+ _services . Remove ( serviceId ) ;
390+ }
384391 }
385- }
386- }
392+
387393
388394 private void AddToManagedServicesIndex ( string serviceName )
389395 {
0 commit comments