Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
AnswerRe: display only first 10 files from each directory Pin
jschell9-Jan-24 12:39
jschell9-Jan-24 12:39 
AnswerRe: display only first 10 files from each directory Pin
OriginalGriff9-Jan-24 19:54
mveOriginalGriff9-Jan-24 19:54 
AnswerRe: display only first 10 files from each directory Pin
Richard Deeming9-Jan-24 23:44
mveRichard Deeming9-Jan-24 23:44 
GeneralRe: display only first 10 files from each directory Pin
OriginalGriff10-Jan-24 23:27
mveOriginalGriff10-Jan-24 23:27 
GeneralRe: display only first 10 files from each directory Pin
Richard Deeming11-Jan-24 0:45
mveRichard Deeming11-Jan-24 0:45 
QuestionCompatibity Pin
PedroSini9-Jan-24 5:06
PedroSini9-Jan-24 5:06 
AnswerRe: Compatibity Pin
Pete O'Hanlon9-Jan-24 5:15
mvePete O'Hanlon9-Jan-24 5:15 
QuestionGet List Of Physical & Logical Drives Pin
Kevin Marois4-Jan-24 22:15
professionalKevin Marois4-Jan-24 22:15 
I'm trying to load a list of drives. If you went to Windows Explorer and clicked on This PC, you would see all of the drives and devices available to you, physically installed, USB, and phones/tablets. This is the list I want

In addition to my C: hard drive, I have a Lenovo tablet, my Samsung S22 Ultra phone, a USB DVD drive, Seagate USB HDD all plugged in.

So, first I use this to get the HD and DVD drives:
var allDrives = DriveInfo.GetDrives();

First Problem
This gives me the drives, but for the DVD drives the DriveType is showing as 'CDRom'.

Second Problem
I want to get tablets and phones that are plugged in. I found this[^], which works well, with one small problem. Based on that I wrote this bit of code that writes out some of the drive meta data:

I get the ClassId from here[^]. Maybe my class Id is wrong??
public static List GetUSBDevices()
{
    var file = @"c:\projects\usbinfo.csv";
    if (File.Exists(file))
    {
        File.Delete(file);
    }

    var devices = new List();

    ManagementObjectCollection collection;
    using (var searcher = new ManagementObjectSearcher("Select * From Win32_PnPEntity"))
    {
        collection = searcher.Get();
    }

    using (var sw = new StreamWriter(file, true))
    {
        var line = "";

        var keys = new string[]
        {
            "Name",
            "Description",
            "Caption",
            "ClassGuid",
        };

        // Write out header line
        foreach (var key in keys)
        {
            line += $"{key},";
        }
        sw.WriteLine(line);
        line = "";

        foreach (var device in collection)
        {
            var name = (string)device.GetPropertyValue("Name");
            var classGuid = (string)device.GetPropertyValue("ClassGuid");

            if (classGuid == "{eec5ad98-8080-425f-922a-dabf3de3f69a}")
            {
                foreach (var key in keys)
                {
                    try
                    {
                        line += $"{(string)device.GetPropertyValue(key)},";
                    }
                    catch { }
                }
                sw.WriteLine(line);
                line = "";
            }
        }
    }

    collection.Dispose();
    return devices;
}
That produced this
Name,Description,Caption,ClassGuid
One Touch,One Touch HDD   ,One Touch,{eec5ad98-8080-425f-922a-dabf3de3f69a}
E:\,STORAGE DEVICE  ,E:\,{eec5ad98-8080-425f-922a-dabf3de3f69a}
Galaxy S22 Ultra,SM-S908U,Galaxy S22 Ultra,{eec5ad98-8080-425f-922a-dabf3de3f69a}
Lenovo Tab M10,Lenovo TB-X505F,Lenovo Tab M10,{eec5ad98-8080-425f-922a-dabf3de3f69a}
Note the last two, the Galaxy S22 Ultra and Lenovo Tab M10 - these I want. The first two are HDD's, which I already have from GetDriveInfo().

So, to recap
1. With GetDriveInfo, how do I know if a drive that says CDRom is really a DVDRom?
2. Is there some way to get just the tablets & phones?

I'm open to a better way of someone has one.

Thanks!
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.

AnswerRe: Get List Of Physical & Logical Drives Pin
Richard Deeming4-Jan-24 22:50
mveRichard Deeming4-Jan-24 22:50 
GeneralRe: Get List Of Physical & Logical Drives Pin
Kevin Marois5-Jan-24 7:32
professionalKevin Marois5-Jan-24 7:32 
QuestionI need to convert a string to a float.... Pin
glennPattonWork33-Jan-24 2:15
professionalglennPattonWork33-Jan-24 2:15 
AnswerRe: I need to convert a string to a float.... Pin
Ron Nicholson3-Jan-24 2:23
professionalRon Nicholson3-Jan-24 2:23 
GeneralRe: I need to convert a string to a float.... Pin
glennPattonWork33-Jan-24 2:29
professionalglennPattonWork33-Jan-24 2:29 
GeneralRe: I need to convert a string to a float.... Pin
OriginalGriff3-Jan-24 5:05
mveOriginalGriff3-Jan-24 5:05 
AnswerRe: I need to convert a string to a float.... Pin
Richard Deeming3-Jan-24 2:30
mveRichard Deeming3-Jan-24 2:30 
GeneralRe: I need to convert a string to a float.... Pin
glennPattonWork33-Jan-24 2:51
professionalglennPattonWork33-Jan-24 2:51 
GeneralToList() or not to List. Pin
Gerry Schmitz16-Dec-23 10:10
mveGerry Schmitz16-Dec-23 10:10 
GeneralRe: ToList() or not to List. Pin
Dave Kreskowiak16-Dec-23 13:44
mveDave Kreskowiak16-Dec-23 13:44 
GeneralRe: ToList() or not to List. Pin
Gerry Schmitz17-Dec-23 5:27
mveGerry Schmitz17-Dec-23 5:27 
GeneralRe: ToList() or not to List. Pin
Dave Kreskowiak17-Dec-23 5:56
mveDave Kreskowiak17-Dec-23 5:56 
GeneralRe: ToList() or not to List. Pin
jschell18-Dec-23 5:54
jschell18-Dec-23 5:54 
GeneralRe: ToList() or not to List. Pin
harold aptroot17-Dec-23 6:15
harold aptroot17-Dec-23 6:15 
GeneralRe: ToList() or not to List. Pin
Pete O'Hanlon18-Dec-23 3:49
mvePete O'Hanlon18-Dec-23 3:49 
Questioncircular slider on winform Pin
sdecorme11-Dec-23 3:23
sdecorme11-Dec-23 3:23 
AnswerRe: circular slider on winform Pin
Ralf Meier11-Dec-23 3:42
mveRalf Meier11-Dec-23 3:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.