The example in this article describes how C # returns all the available drive symbols for the current system. Share to everyone for your reference. as follows:
The initial C # code for the "plain" WMI query is generated by//WMI code generator, Version 5.00,//HTTP://WWW.ROBVA
nderwoude.com/wmigen.php using System;
Using System.Management;
Using System.Collections.Generic; Namespace Robvanderwoude {public class Listdrives {public static int Main (string[] args) {try {Strin G computer = string.
Empty; #region Command Line Parsing//Only 1 optional Argument allowed:a remote computer name if (args.
Length > 1) {throw new Exception ("Invalid command line Arguments"); } if (args. Length = = 1) {//We ll display a ' friendly ' message if help is requested if (args[0). StartsWith ("/") | | Args[0]. StartsWith ("-")) {switch (args[0]. ToUpper ()) {case '/? ': Case '-? ': Case '/h ': Case "-H": Case "--h": CAs E "/help": Case "-help": Case "--help": Return Writeerror (String.
Empty); Default
Return writeerror ("Invalid command line argument");
} else {computer = "\\\\" + args[0] + "\";
}} #endregion String wmins = computer + "root\\cimv2";
ManagementObjectSearcher searcher = new ManagementObjectSearcher (wmins, "select * from Win32_LogicalDisk");
list<string> drives = new list<string> (); foreach (ManagementObject queryobj in searcher. Get ()) {drives. ADD (queryobj["DeviceID"].
ToString ()); } drives.
Sort ();
String drivelist = "";
foreach (string drive in drives) {drivelist = = (drive + ""); } Console.WriteLine (Drivelist.
Trim ());
return 0;
catch (Exception e) {return writeerror (e);
The public static int writeerror (Exception e) {return writeerror (E = null? null:e.message); public static int Writeerror (String errormessage) {string fullpath = Environment.getcommandlineargs (). GetvalUE (0).
ToString (); string[] program = FullPath.
Split (' \ \ '); String exename = Program[program.
GetUpperBound (0)]; EXEName = exename. Substring (0, EXEName.
IndexOf ('. ')); if (string.
IsNullOrEmpty (errormessage) = = False) {Console.Error.WriteLine ();
Console.foregroundcolor = consolecolor.red;
Console.Error.Write ("Error:");
Console.foregroundcolor = Consolecolor.white;
Console.Error.WriteLine (errormessage);
Console.resetcolor ();
} Console.Error.WriteLine ();
Console.Error.WriteLine (EXEName + ", Version 1.10");
Console.Error.WriteLine ("List all drive letters in" to the specified computer);
Console.Error.WriteLine ();
Console.Error.Write ("Usage:");
Console.foregroundcolor = Consolecolor.white; Console.Error.Write (exename.
ToUpper ());
Console.Error.WriteLine ("[ComputerName]");
Console.resetcolor ();
Console.Error.WriteLine (); Console.Error.WriteLine ("Where: ' ComputerName ' is" Optional) name of a remote computer ");
Console.Error.WriteLine ("(Default if not specified:local computer)");
Console.Error.WriteLine ();
Console.Error.WriteLine ("written by Rob van der Woude");
return 1; }
}
}
I hope this article will help you with your C # programming.