Any alternative solutions to this

G

Guest

Hi,
i am new to Visual C#.
i am trying to develop a file searcher utility similar to that of Windows
own 'Search for files and folder' for desktop.
i want to know is there a method that can return all the valid hard
disk/partitions on a system, so i can have a option like 'Local harddrives'
in my program.
i have used 'Directory.GetLogicalDrives()' but it also returns floppy and CD
drives letters as well.

i faced the same result when i used 'WqlObjectQuery("select * from
Win32_LogicalDisk")'.i must mention i am new to WMI as well.
However i have succeeded in isolating the logical drives by checking
"DriveType" property of Management Objects returned by this query.

WqlObjectQuery objectQuery = new WqlObjectQuery("select * from
Win32_LogicalDisk");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);
ManagementObjectCollection disks = searcher.Get();
foreach (ManagementObject disk in disks)
{
if(disk["DriveType"].ToString() == "3")
{
Console.WriteLine("Disk = " + disk["deviceid"]);
}
}

The query takes considerable time to show the results so i was wondering if
there is a better way of getting the same result or speeding up execution of
this particular query. I dont want to use any unmanaged DLL to solve this.
Thanks for any help/hints offered.
 
J

Jonathan Allen

i am new to Visual C#.

You could have fooled me, I wouldn't have found Wql so easily. Thanks for
introducing it to me.

As for your problem, you were so close its funny. All you have to do is add
"WHERE DriveType=3" to your query. Don't you love those rare cases where
things just work.?


As for the speed issue, it only seems slow the first time I run it. After
that, it returns pretty quick.
 
D

Dmitry Klymenko

One more point of usage of WMI (WqlObjectQuery("select * from Win32_LogicalDisk"))
This will not work on client computer if it has service Windows Management.. disabled.

Try to use PInvoke and some of kernel32.dll functions. Any way both of this behavior (except Directory.GetLogicalDrives()) will never twilight be ported.
Hi,
i am new to Visual C#.
i am trying to develop a file searcher utility similar to that of Windows
own 'Search for files and folder' for desktop.
i want to know is there a method that can return all the valid hard
disk/partitions on a system, so i can have a option like 'Local harddrives'
in my program.
i have used 'Directory.GetLogicalDrives()' but it also returns floppy and CD
drives letters as well.

i faced the same result when i used 'WqlObjectQuery("select * from
Win32_LogicalDisk")'.i must mention i am new to WMI as well.
However i have succeeded in isolating the logical drives by checking
"DriveType" property of Management Objects returned by this query.

WqlObjectQuery objectQuery = new WqlObjectQuery("select * from
Win32_LogicalDisk");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);
ManagementObjectCollection disks = searcher.Get();
foreach (ManagementObject disk in disks)
{
if(disk["DriveType"].ToString() == "3")
{
Console.WriteLine("Disk = " + disk["deviceid"]);
}
}

The query takes considerable time to show the results so i was wondering if
there is a better way of getting the same result or speeding up execution of
this particular query. I dont want to use any unmanaged DLL to solve this.
Thanks for any help/hints offered.
 
W

Willy Denoyette [MVP]

Any reason why a client would disable this service? To disable the service you need administrator privileges, do your clients run as administrator?, well seems you don't care about security at all.
The WMI service is a system service that's needed by other system components, it should not be disabled, unless you know the consequences of what you are doing. Heck, you don't disable the lssa service or the plug and play service as well do you?
Don't know for sure what is meant by this - Any way both of this behavior will never twilight be ported.
So please stop forcing people to PInvoke low level Win32 API's when higher level managed OO solutions are available (System.Management namespace is managed code).

Willy.


One more point of usage of WMI (WqlObjectQuery("select * from Win32_LogicalDisk"))
This will not work on client computer if it has service Windows Management.. disabled.

Try to use PInvoke and some of kernel32.dll functions. Any way both of this behavior (except Directory.GetLogicalDrives()) will never twilight be ported.
Hi,
i am new to Visual C#.
i am trying to develop a file searcher utility similar to that of Windows
own 'Search for files and folder' for desktop.
i want to know is there a method that can return all the valid hard
disk/partitions on a system, so i can have a option like 'Local harddrives'
in my program.
i have used 'Directory.GetLogicalDrives()' but it also returns floppy and CD
drives letters as well.

i faced the same result when i used 'WqlObjectQuery("select * from
Win32_LogicalDisk")'.i must mention i am new to WMI as well.
However i have succeeded in isolating the logical drives by checking
"DriveType" property of Management Objects returned by this query.

WqlObjectQuery objectQuery = new WqlObjectQuery("select * from
Win32_LogicalDisk");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);
ManagementObjectCollection disks = searcher.Get();
foreach (ManagementObject disk in disks)
{
if(disk["DriveType"].ToString() == "3")
{
Console.WriteLine("Disk = " + disk["deviceid"]);
}
}

The query takes considerable time to show the results so i was wondering if
there is a better way of getting the same result or speeding up execution of
this particular query. I dont want to use any unmanaged DLL to solve this.
Thanks for any help/hints offered.
 
W

Willy Denoyette [MVP]

sysdeamon said:
Hi,
i am new to Visual C#.
i am trying to develop a file searcher utility similar to that of Windows
own 'Search for files and folder' for desktop.
i want to know is there a method that can return all the valid hard
disk/partitions on a system, so i can have a option like 'Local
harddrives'
in my program.
i have used 'Directory.GetLogicalDrives()' but it also returns floppy and
CD
drives letters as well.

i faced the same result when i used 'WqlObjectQuery("select * from
Win32_LogicalDisk")'.i must mention i am new to WMI as well.
However i have succeeded in isolating the logical drives by checking
"DriveType" property of Management Objects returned by this query.

WqlObjectQuery objectQuery = new WqlObjectQuery("select * from
Win32_LogicalDisk");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);
ManagementObjectCollection disks = searcher.Get();
foreach (ManagementObject disk in disks)
{
if(disk["DriveType"].ToString() == "3")
{
Console.WriteLine("Disk = " + disk["deviceid"]);
}
}

The query takes considerable time to show the results so i was wondering
if
there is a better way of getting the same result or speeding up execution
of
this particular query. I dont want to use any unmanaged DLL to solve this.
Thanks for any help/hints offered.

Do as Jonathan told you, the query should not take more than a few ten
millisecs.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top