Get USB Info with impersonation is not working

Dani_S 4,461 Reputation points
2025-04-22T14:06:52.93+00:00

Hi,

Without impersonation I get USB info.

With impersonation is not working.

I'm using .NET 9 , MAUI on windows.

I got driver is not ready!!! Can you please help ?

My code with impersonation is :

            `if (LoginConfigurations.GetInstance().IsUserDeviceControl &&`
````                  SelectedSourceDriveItem.DriveInfoValue.DriveType == DriveType.Removable)`

`                {`

`                    var drive = SelectedSourceDriveItem?.DriveInfoValue;`

`                    UsbInfoMessage = string.Empty;`

`                    if (drive.IsReady)`

`                    {`

`#if WINDOWS`

`                        WindowsIdentity.RunImpersonated(`

`                     ImpersonationHelper.Token,`

`                            () =>`

`                            {`

`                                string driveLetterName = drive.Name;`

`                                string driveFormat = drive.DriveFormat;`

`                                string driveType = drive.DriveType.ToString();`

`                                string volumeName = string.IsNullOrEmpty(drive.VolumeLabel) ? "No Label" : drive.VolumeLabel;`

`                                long totalSize = drive.TotalSize;`

`                                long freeSpace = drive.TotalFreeSpace;`

`                                long usedSpace = totalSize - freeSpace;`

`                                StringBuilder stringBuilder = new StringBuilder();`

`                                stringBuilder.AppendLine($"Drive Name: {driveLetterName}");`

`                                stringBuilder.AppendLine($"Drive Type: {driveFormat}");`

`                                stringBuilder.AppendLine($"File System: {driveType}");`

`                                stringBuilder.AppendLine($"Volume Label: {volumeName}");`

`                                stringBuilder.AppendLine($"Total Size: {FormatBytes(totalSize)}");`

`                                stringBuilder.AppendLine($"Used Space: {FormatBytes(usedSpace)}");`

`                                stringBuilder.AppendLine($"Free Space: {FormatBytes(freeSpace)}");`

`                                UsbInfoMessage = stringBuilder.ToString();`

`                                PopupBorderColor = Color.FromRgb(255, 255, 0);`

`                                IsOpenUsbInfoPopup = true;`

`                            });`

`#endif`

`                    }`

`                    else`

`                    {`

`                        IsBusyBusyIndicator = false;`

`                        IsOpenPopup = true;`

`                        PopupBorderColor = Color.FromRgb(255, 0, 0);`

`                        ErrorMessage = $"Get USB info does not succeed.";`

`                        _logger.Error(ErrorMessage + " Since usb is not ready");`

`                    }`

`                }`

     
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,230 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 75,051 Reputation points
    2025-04-22T16:14:14.91+00:00

    it is probably a mapped drive (user mounted to drive letter). the mapping by drive letter is only available to the account that mapped it. an impersonation account must use the UNC path.


  2. Dani_S 4,461 Reputation points
    2025-04-23T08:39:44.1033333+00:00

    This code is work.

                if (LoginConfigurations.GetInstance().IsUserDeviceControl &&
    
                  SelectedSourceDriveItem.DriveInfoValue.DriveType == DriveType.Removable)
    
                {
    
                    var driveInfo = SelectedSourceDriveItem?.DriveInfoValue;
    
                    UsbInfoMessage = string.Empty;
    

    #if WINDOWS

                    WindowsIdentity.RunImpersonated(
    
                 ImpersonationHelper.Token,
    
                        () =>
    
                        {
    
                            var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 2"); // 2 = Removable
    
                           foreach (ManagementObject drive in searcher.Get())
    
                    {
    
                        var letter = drive["DeviceID"];
    
                        if(letter.ToString() != driveInfo.Name.TrimEnd('\\'))
    
                        {
    
                            continue;
    
                        }
    
                        var totalSize = Convert.ToInt64(drive["Size"]);
    
                        var freeSpace = Convert.ToInt64(drive["FreeSpace"]);
    
                        long usedSpace = totalSize - freeSpace;
    
                        StringBuilder stringBuilder = new StringBuilder();
    
                        stringBuilder.AppendLine($"Drive Name: {letter.ToString()}");
    
                        stringBuilder.AppendLine($"File System: {drive["FileSystem"].ToString()}");
    
                        stringBuilder.AppendLine($"Total Size: {FormatBytes(totalSize)}");
    
                        stringBuilder.AppendLine($"Used Space: {FormatBytes(usedSpace)}");
    
                        stringBuilder.AppendLine($"Free Space: {FormatBytes(freeSpace)}");
    
                        UsbInfoMessage = stringBuilder.ToString();
    
                        PopupBorderColor = Color.FromRgb(255, 255, 0);
    
                        IsOpenUsbInfoPopup = true;
    
                      
    
                    }
    
                        });
    

    #endif

                }
    
        
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.