.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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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");`
` }`
` }`
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.
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
}