C # methods to return all available drive symbols for the current system _python

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.