@@ -462,42 +462,49 @@ public enum FileType : uint
462462 Pipe = FILE_TYPE . FILE_TYPE_PIPE ,
463463 }
464464
465- public override string ToString ( ) => ToString ( true ) ;
465+ public override string ToString ( ) => ToString ( false , false ) ;
466466
467467 /// <summary>
468468 /// Get the string representation of this SafeFileHandleEx object.
469469 /// </summary>
470- /// <param name="init ">If TRUE, get values from Properties. If FALSE, get values from Properties' backing fields.</param>
470+ /// <param name="initProps ">If TRUE, get values from Properties. If FALSE, get values from Properties' backing fields.</param>
471471 /// <returns>The string representation of this SafeFileHandleEx object.</returns>
472- public string ToString ( bool init )
472+ public string ToString ( bool initProps , bool initProcessInfo )
473473 {
474- string [ ] exLog = ExceptionLog . ConvertAll ( ex => ex . ToString ( ) ) . ToArray ( ) ;
475- for ( int i = 0 ; i < exLog . Length ; i ++ )
474+ try
476475 {
477- exLog [ i ] = $ " { exLog [ i ] } ". Replace ( "\n " , "\n " ) + "\r \n " ;
478- }
476+ string [ ] exLog = ExceptionLog . ConvertAll ( ex => ex . ToString ( ) ) . ToArray ( ) ;
477+ for ( int i = 0 ; i < exLog . Length ; i ++ )
478+ {
479+ exLog [ i ] = $ " { exLog [ i ] } ". Replace ( "\n " , "\n " ) + "\r \n " ;
480+ }
479481
480- return @$ "{ nameof ( SafeFileHandleEx ) } hash:{ GetHashCode ( ) }
482+ return @$ "{ nameof ( SafeFileHandleEx ) } hash:{ GetHashCode ( ) }
481483 { nameof ( CreatorBackTraceIndex ) } : { CreatorBackTraceIndex }
482- { nameof ( FileFullPath ) } : { ( init ? ( FileFullPath . v ?? FileFullPath . ex ? . ToString ( ) ) : ( fileFullPath . v ?? fileFullPath . ex ? . ToString ( ) ) ) }
483- { nameof ( FileHandleType ) } : { ( init ? ( FileHandleType . v ? . ToString ( ) ?? FileFullPath . ex ? . ToString ( ) ) : ( fileHandleType . v ? . ToString ( ) ?? fileHandleType . ex ? . ToString ( ) ) ) }
484- { nameof ( FileName ) } : { ( init ? ( FileName . v ?? FileName . ex ? . ToString ( ) ) : ( fileName . v ?? fileName . ex ? . ToString ( ) ) ) }
484+ { nameof ( FileFullPath ) } : { ( initProps ? ( FileFullPath . v ?? FileFullPath . ex ? . ToString ( ) ) : ( fileFullPath . v ?? fileFullPath . ex ? . ToString ( ) ) ) }
485+ { nameof ( FileHandleType ) } : { ( initProps ? ( FileHandleType . v ? . ToString ( ) ?? FileFullPath . ex ? . ToString ( ) ) : ( fileHandleType . v ? . ToString ( ) ?? fileHandleType . ex ? . ToString ( ) ) ) }
486+ { nameof ( FileName ) } : { ( initProps ? ( FileName . v ?? FileName . ex ? . ToString ( ) ) : ( fileName . v ?? fileName . ex ? . ToString ( ) ) ) }
485487 { nameof ( GrantedAccess ) } : { SysHandleEx . GrantedAccessString }
486- { nameof ( HandleObjectType ) } : { ( init ? ( HandleObjectType . v ?? HandleObjectType . ex ? . ToString ( ) ) : ( handleObjectType . v ?? handleObjectType . ex ? . ToString ( ) ) ) }
488+ { nameof ( HandleObjectType ) } : { ( initProps ? ( HandleObjectType . v ?? HandleObjectType . ex ? . ToString ( ) ) : ( handleObjectType . v ?? handleObjectType . ex ? . ToString ( ) ) ) }
487489 { nameof ( HandleValue ) } : { HandleValue } (0x{ HandleValue : X} )
488490 { nameof ( IsClosed ) } : { IsClosed }
489- { nameof ( IsDirectory ) } : { ( init ? ( IsDirectory . v ? . ToString ( ) ?? IsDirectory . ex ? . ToString ( ) ) : ( isDirectory . v ? . ToString ( ) ?? isDirectory . ex ? . ToString ( ) ) ) }
490- { nameof ( IsFileHandle ) } : { ( init ? ( IsFileHandle . v ? . ToString ( ) ?? IsFileHandle . ex ? . ToString ( ) ) : ( isFileHandle . v ? . ToString ( ) ?? isFileHandle . ex ? . ToString ( ) ) ) }
491+ { nameof ( IsDirectory ) } : { ( initProps ? ( IsDirectory . v ? . ToString ( ) ?? IsDirectory . ex ? . ToString ( ) ) : ( isDirectory . v ? . ToString ( ) ?? isDirectory . ex ? . ToString ( ) ) ) }
492+ { nameof ( IsFileHandle ) } : { ( initProps ? ( IsFileHandle . v ? . ToString ( ) ?? IsFileHandle . ex ? . ToString ( ) ) : ( isFileHandle . v ? . ToString ( ) ?? isFileHandle . ex ? . ToString ( ) ) ) }
491493 { nameof ( IsInvalid ) } : { IsInvalid }
492494 { nameof ( ObjectAddress ) } : { ObjectAddress } (0x{ ObjectAddress : X} )
493- { nameof ( ObjectName ) } : { ( init ? ( ObjectName . v ?? ObjectName . ex ? . ToString ( ) ) : ( objectName . v ?? objectName . ex ? . ToString ( ) ) ) }
495+ { nameof ( ObjectName ) } : { ( initProps ? ( ObjectName . v ?? ObjectName . ex ? . ToString ( ) ) : ( objectName . v ?? objectName . ex ? . ToString ( ) ) ) }
494496 { nameof ( ProcessId ) } : { ProcessId }
495- { nameof ( ProcessInfo . ParentId ) } : { ProcessInfo . ParentId . v ? . ToString ( ) ?? ProcessInfo . ParentId . ex ? . ToString ( ) ?? string . Empty }
496- { nameof ( ProcessInfo . ProcessCommandLine ) } : { ProcessInfo . ProcessCommandLine . v ?? ProcessInfo . ProcessCommandLine . ex ? . ToString ( ) }
497- { nameof ( ProcessInfo . ProcessMainModulePath ) } : { ProcessInfo . ProcessMainModulePath . v ?? ProcessInfo . ProcessMainModulePath . ex ? . ToString ( ) }
498- { nameof ( ProcessInfo . ProcessName ) } : { ProcessInfo . ProcessName . v ?? ProcessInfo . ProcessName . ex ? . ToString ( ) }
499- { nameof ( ProcessInfo . ProcessProtection ) } : { ProcessInfo . ProcessProtection . v ? . ToString ( ) ?? ProcessInfo . ProcessProtection . ex ? . ToString ( ) ?? string . Empty }
497+ { nameof ( ProcessInfo . ParentId ) } : { ( initProcessInfo ? ( ProcessInfo . ParentId . v ? . ToString ( ) ?? ProcessInfo . ParentId . ex ? . ToString ( ) ) : ( processInfo ? . ParentId . v ? . ToString ( ) ?? processInfo ? . ParentId . ex ? . ToString ( ) ?? string . Empty ) ) }
498+ { nameof ( ProcessInfo . ProcessCommandLine ) } : { ( initProcessInfo ? ( ProcessInfo . ProcessCommandLine . v ?? ProcessInfo . ProcessCommandLine . ex ? . ToString ( ) ) : ( processInfo ? . ProcessCommandLine . v ?? processInfo ? . ProcessCommandLine . ex ? . ToString ( ) ?? string . Empty ) ) }
499+ { nameof ( ProcessInfo . ProcessMainModulePath ) } : { ( initProcessInfo ? ( ProcessInfo . ProcessMainModulePath . v ?? ProcessInfo . ProcessMainModulePath . ex ? . ToString ( ) ) : ( processInfo ? . ProcessMainModulePath . v ?? processInfo ? . ProcessMainModulePath . ex ? . ToString ( ) ?? string . Empty ) ) }
500+ { nameof ( ProcessInfo . ProcessName ) } : { ( initProcessInfo ? ( ProcessInfo . ProcessName . v ?? ProcessInfo . ProcessName . ex ? . ToString ( ) ) : ( processInfo ? . ProcessName . v ?? processInfo ? . ProcessName . ex ? . ToString ( ) ?? string . Empty ) ) }
501+ { nameof ( ProcessInfo . ProcessProtection ) } : { ( initProcessInfo ? ( ProcessInfo . ProcessProtection . v ? . ToString ( ) ?? ProcessInfo . ProcessProtection . ex ? . ToString ( ) ) : ( processInfo ? . ProcessProtection . v ? . ToString ( ) ?? processInfo ? . ProcessProtection . ex ? . ToString ( ) ?? string . Empty ) ) }
500502 { nameof ( ExceptionLog ) } : ...
501503 " + string . Concat ( exLog ) ;
504+ }
505+ catch ( Exception ex )
506+ {
507+ return $ "Error while evaluating properties for SafeFileHandleEx.ToString(): { ex } ";
508+ }
502509 }
503510}
0 commit comments