Get all drives in MAC ?
Hi Bruce,
Maui approach is to run windows in a vm, and use visual studio for windows.
How i get in C# all the drives in MAC ?
Thanks in advance,
.NET MAUI
-
Dani_S 3,026 Reputation points
2024-05-14T03:30:52.98+00:00 and what kind of drives i have in MAC?
-
Bruce (SqlWork.com) 58,936 Reputation points
2024-05-14T15:39:45.4333333+00:00 Your question is not clear. Unix has a unified file system. Drives are mounted into file system, so you just use directory services. macOS supports both auto mounts and hard mounts.
if you programmatically want device info, then you would use IOKit sdk.
note: as Maui build catalyst apps, you have limited permissions.
-
Dani_S 3,026 Reputation points
2024-05-14T17:14:18.4466667+00:00 Can you please supply code sniped how to get programmatically the device info,
use IOKit sdk?
-
Bruce (SqlWork.com) 58,936 Reputation points
2024-05-14T21:09:53.59+00:00 what info do you want? the standard unix approach would be to spawn (fork) the ioreg or system_profiler utility and parse the output:
http://www.manpagez.com/man/8/ioreg/ https://ss64.com/mac/system_profiler.html
example - usb device info:
% ioreg -p IOUSB +-o Root <class IORegistryEntry, id 0x100000100, retain 30> +-o AppleT8103USBXHCI@00000000 <class AppleT8103USBXHCI, id 0x1000004ed, registered, matched, active, busy 0 (13 ms), retain 39> +-o AppleT8103USBXHCI@01000000 <class AppleT8103USBXHCI, id 0x10000037f, registered, matched, active, busy 0 (2 ms), retain 39> +-o AppleUSBXHCITR@02000000 <class AppleUSBXHCITR, id 0x1000a7757, registered, matched, active, busy 0 (1226 ms), retain 375> +-o USB2 Hub@02100000 <class IOUSBHostDevice, id 0x1000a7762, registered, matched, active, busy 0 (1215 ms), retain 36> | +-o Studio Display@02140000 <class IOUSBHostDevice, id 0x1000a777f, registered, matched, active, busy 0 (882 ms), retain 159> | +-o USB Flash Disk @02130000 <class IOUSBHostDevice, id 0x1000dc51b, registered, matched, active, busy 0 (118 ms), retain 29> +-o USB3 Gen2 Hub@02300000 <class IOUSBHostDevice, id 0x1000a7771, registered, matched, active, busy 0 (256 ms), retain 33> +-o INIC-3609 @02310000 <class IOUSBHostDevice, id 0x1000a778c, registered, matched, active, busy 0 (251 ms), retain 45>
example - list USB device name only:
% ioreg -p IOUSB -w0 | sed 's/[^o]*o //; s/@.*$//' | grep -v '^Root.*' AppleT8103USBXHCI AppleT8103USBXHCI AppleUSBXHCITR USB2 Hub Studio Display USB Flash Disk USB3 Gen2 Hub INIC-3609
example usb profile in json:
% system_profiler SPUSBDataType -json { "SPUSBDataType" : [ { "_name" : "USB31Bus", "host_controller" : "AppleT8103USBXHCI" }, { "_name" : "USB31Bus", "host_controller" : "AppleT8103USBXHCI" }, { "_items" : [ { "_items" : [ { "_name" : "INIC-3609 ", "bcd_device" : "1.40", "bus_power" : "900", "bus_power_used" : "144", "device_speed" : "super_speed", "extra_current_used" : "0", "location_id" : "0x02310000 / 4", "manufacturer" : "Initio ", "product_id" : "0x3940", "serial_num" : "************", "vendor_id" : "0x13fd (Initio Corporation)" } ], "_name" : "USB3 Gen2 Hub", "bcd_device" : "52.12", "bus_power" : "900", "bus_power_used" : "0", "device_speed" : "super_speed_plus", "extra_current_used" : "0", "location_id" : "0x02300000 / 2", "manufacturer" : "Apple", "product_id" : "0x101e", "serial_num" : "***********", "vendor_id" : "apple_vendor_id" }, { "_items" : [ { "_name" : "USB Flash Disk ", "bcd_device" : "1.00", "bus_power" : "500", "bus_power_used" : "500", "device_speed" : "high_speed", "extra_current_used" : "0", "location_id" : "0x02130000 / 7", "manufacturer" : "General ", "Media" : [ { "_name" : "USB Flash Disk", "bsd_name" : "disk16", "Logical Unit" : 0, "partition_map_type" : "master_boot_record_partition_map_type", "removable_media" : "yes", "size" : "252.7 MB", "size_in_bytes" : 252706816, "smart_status" : "Verified", "USB Interface" : 0, "volumes" : [ { "_name" : "NO NAME", "bsd_name" : "disk16s1", "file_system" : "MS-DOS FAT16", "free_space" : "184.6 MB", "free_space_in_bytes" : 184586240, "iocontent" : "DOS_FAT_16", "mount_point" : "/Volumes/NO NAME", "size" : "252.7 MB", "size_in_bytes" : 252690432, "volume_uuid" : "5190990C-0600-352D-8410-858A283489CE", "writable" : "yes" } ] } ], "product_id" : "0x800b", "serial_num" : "*****", "vendor_id" : "0x8644" }, { "_name" : "Studio Display", "bcd_device" : "2.01", "bus_power" : "500", "bus_power_used" : "500", "device_speed" : "high_speed", "extra_current_used" : "0", "location_id" : "0x02140000 / 3", "manufacturer" : "Apple Inc.", "product_id" : "0x1114", "serial_num" : "*********************", "vendor_id" : "apple_vendor_id" } ], "_name" : "USB2 Hub", "bcd_device" : "52.12", "bus_power" : "500", "bus_power_used" : "0", "device_speed" : "high_speed", "extra_current_used" : "0", "location_id" : "0x02100000 / 1", "manufacturer" : "Apple", "product_id" : "0x101d", "serial_num" : "************", "vendor_id" : "apple_vendor_id" } ], "_name" : "USB31Bus", "host_controller" : "AppleUSBXHCITR", "pci_device" : "0x15f0 ", "pci_revision" : "0x0006 ", "pci_vendor" : "0x8086 " } ] }
-
Dani_S 3,026 Reputation points
2024-05-23T06:12:23.9033333+00:00 Hi,
There is no code in C# to get all the drives and volumes?
In windows i did :
DriveInfo[] drives = DriveInfo.GetDrives();
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,596 Reputation points • Microsoft Vendor
2024-05-24T06:11:19.4733333+00:00 About IOKit, you can see IOKit | Apple Developer Documentation
-
Dani_S 3,026 Reputation points
2024-05-24T10:39:08.3766667+00:00 can you please a sniped code?
-
Dani_S 3,026 Reputation points
2024-05-24T10:39:28.55+00:00 In csharp?
-
Bruce (SqlWork.com) 58,936 Reputation points
2024-05-25T04:28:51.0566667+00:00 Here is GitHub library with samples. Not sure it’s maintained.
https://github.com/CartBlanche/IOKit-Sharp?tab=readme-ov-file
-
Dani_S 3,026 Reputation points
2024-05-26T17:50:10.5633333+00:00 Can you please give a code how to do that ?
-
Bruce (SqlWork.com) 58,936 Reputation points
2024-05-27T22:20:51.6466667+00:00 code to do what? what kind of information are you looking for? what kind of devices and volumes? also do you understand what drives and volumes are on unix system? a volume can be a mountable file, like most installers.
unix also supports automount. that is the first reference to the directory mount point causes the filesystem (local device or network) to mount.
if you explained the problem you are trying to solve, and the Maui platform, IOS, MacCatalyst, etc, we could give better help. In general unix hides the implementation of devices and volumes. They are mounted into the directory structure, and accessed via the file system.
note: IOKit is written in a restricted subset of C++. It is the base library for writing device drivers. It should be easily callable via p-invoke from c#, though I would not try to write a driver in c#.
-
Dani_S 3,026 Reputation points
2024-05-29T07:26:22.41+00:00 Hi Bruce,
I added screenshhot to see what I'm looking for:
https://app.screencast.com/fUzHs7VJVYjv5
In window Ichoose drive and later I'm show you all the folders and files.
How is can done in mac?
Can you supply code in C#?
Thanks in adavance,
-
Bruce (SqlWork.com) 58,936 Reputation points
2024-05-29T20:27:36.15+00:00 I can't see your video. but there is no concept of a driver letter on macOS, and drives are numbered /dev/disk<number>. but this is just the physical device, to see the filesystem, you access the mount point in the /Volumes folder.
for your app, you just use the maui file picker:
-
Dani_S 3,026 Reputation points
2024-05-30T08:58:47.0166667+00:00 Here is the results of left side picker using this code:
DriveInfo[] drives = DriveInfo.GetDrives();
drives = drives.Where(drive=> drive.DriveType != DriveType.Removable && drive.DriveType != DriveType.CDRom).ToArray();
I thing I got physical device that are not needed- how do I fix it?
To see file system i used the name of the physical device and used in reucrsive
get directories and files.
My aim in this screen is to copy from all available volumes folders to same available volumes folders + usb and CDROM also.
And the antivirus willenable only the application to reach usb and CDROM.
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,596 Reputation points • Microsoft Vendor
2024-06-04T05:23:46.7133333+00:00 The video cannot be opened. What's the left side picker? Are you using FilePicker to select a file according to Bruce's advance?
-
Dani_S 3,026 Reputation points
2024-06-04T06:11:30.34+00:00 Yes file picker
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,596 Reputation points • Microsoft Vendor
2024-06-05T07:25:05.46+00:00 I thing I got physical device that are not needed
Currently, you can access the device but some of them are not needed, right?
I‘m confused about your main purpose, do you have issue when picking files via file picker? Can your pick the file in your device?
-
Dani_S 3,026 Reputation points
2024-06-05T08:00:01.5933333+00:00 Currently, you can access the device but some of them are not needed, right? yes
i want to see only the volumes that are not usb or CDRom.
i do not want user insert malicious files into system.
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,596 Reputation points • Microsoft Vendor
2024-06-06T05:57:16.4233333+00:00 You don't want all file types to be selectable, right? If so, you would specify file types like the official sample- File picker - .NET MAUI | Microsoft Learn
For example:
var customFileType = new FilePickerFileType( new Dictionary<DevicePlatform, IEnumerable<string>> { { DevicePlatform.iOS, new[] { "public.my.comic.extension" } }, // UTType values { DevicePlatform.Android, new[] { "application/comics" } }, // MIME type { DevicePlatform.WinUI, new[] { ".txt" } }, //this will only your app select text file, you can set file extensions according to your needs { DevicePlatform.Tizen, new[] { "*/*" } }, { DevicePlatform.macOS, new[] { "cbr", "cbz" } }, // UTType values });
-
Dani_S 3,026 Reputation points
2024-06-06T07:35:03.1166667+00:00 No .
i want to see only the volumes that are not usb or CDRom.
i do not want user insert malicious files into system.
-
Bruce (SqlWork.com) 58,936 Reputation points
2024-06-06T21:46:02.3266667+00:00 if you don't want usb drives or cd, then why allow anything outside $HOME/ as a source?
there may be links in $HOME to folders you may want to exclude. for example One Drive is usually linked as $HOME/<one drive name>. the actual data is in ~/Library/CloudStorage/<one drive name>.
note: desktop and documents are often mapped to the iCloud volume
Sign in to comment