C#返回当前系统所有可用驱动器符号

C#返回当前系统所有可用驱动器符号,第1张

概述C#返回当前系统所有可用驱动器符号

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

// The initial C# code for the "plain" WMI query was generated by// WMI Code Generator,Version 5.00,http://www.robvanderwoude.com/wmigen.PHP using System;using System.Management;using System.Collections.Generic; namespace RobvanderWoude{    public class ListDrives    {        public static int Main( string[] args )        {            try            {                string 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 was requested                    if ( args[0].StartsWith( "/" ) || args[0].StartsWith( "-" ) )                    {                        switch ( args[0].toupper( ) )                        {                            case "/?":                            case "-?":                            case "/H":                            case "-H":                            case "--H":                            case "/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 );            }        }         public static int WriteError( Exception e )        {            return WriteError( e == null ? null : e.Message );        }         public static int WriteError( string errorMessage )        {            /*            ListDrives,Version 1.10            List all drive letters in use on the specifIEd computer             Usage:  ListDRIVES  [ computername ]             Where:  'computername'  is the (optional) name of a remote computer                                    (default if not specifIEd: local computer)             Written by Rob van der Woude            http://www.robvanderwoude.com            */             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 use on 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 the (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" );            Console.Error.Writeline( "http://www.robvanderwoude.com" );            return 1;        }    }}

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的C#返回当前系统所有可用驱动器符号全部内容,希望文章能够帮你解决C#返回当前系统所有可用驱动器符号所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1237795.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存