C# DriveInfo – Class: System.IO Namespace हमें DriveInfo नाम की एक Class Provide करता है, जिसके DriveInfo.GetDrives() Method का प्रयोग करके हम हमारे Computer के सभी Drives का नाम Retrieve कर सकते हैं।
साथ ही हम हमारे Drive से सम्बंधित विभिन्न प्रकार की अन्य जानकारियां जैसे कि Drive Type, Available Free Space व Volume Label की Information भी Retrieve कर सकते हैं। जैसे:
File Name: DriveInformation.cs using System.IO; using System; namespace CSharpFilesAndStreams { class Program { static void Main(string[] args) { // Get info regarding all drives. DriveInfo[] myDrives = DriveInfo.GetDrives(); // Now print drive stats. foreach (DriveInfo d in myDrives) { Console.WriteLine("Name: {0}", d.Name); Console.WriteLine("Type: {0}", d.DriveType); // Check to see whether the drive is mounted. if (d.IsReady) { Console.WriteLine("Free space: {0}", d.TotalFreeSpace); Console.WriteLine("Format: {0}", d.DriveFormat); Console.WriteLine("Label: {0}", d.VolumeLabel); } Console.WriteLine(); } Console.ReadLine(); } } } // Output: Name: C:\ Type: Fixed Free space: 3004350464 Format: NTFS Label: Name: D:\ Type: CDRom Name: E:\ Type: Fixed Free space: 5080743936 Format: NTFS Label: My Documents Name: F:\ Type: Fixed Free space: 1849143296 Format: NTFS Label: Multimedia Name: G:\ Type: Fixed Free space: 2429181952 Format: NTFS Label: Software Name: H:\ Type: CDRom
इस Program में सबसे पहले हमने हमारे Computer System की सभी Derives के References को एक myDrives नाम के DriveInfo Type के Array में Store किया है क्योंकि जब हम DriveInfo.GetDrives() Method को Call करते हैं, तो ये Method हमारे Current Computer के सभी Drives को एक DriveInfo Object के Array के रूप में Return करता है:
DriveInfo[] myDrives = DriveInfo.GetDrives();
चूंकि myDrives एक Array है इसलिए इसके विभिन्न Objects को अलग-अलग Access करने के लिए हमने एक foreach Loop Use किया है और Drive Related विभिन्न Members को Use करके Drive से सम्बंधित Information को Output में Display किया है।
हम एक बार फिर याद दिला देना चाहते हैं कि .NET Framework के विभिन्न Types के Members की Detailed Information प्राप्त करने के लिए Visual Studio के Object Browser Tool को Use करना चाहिए। क्योंकि किसी भी Class के सभी Members के बारे में इस पुस्तक में Discuss करना सम्भव नहीं हैं।
इसलिए हम केवल Classes को किस तरह से Use किया जा सकता है और किस तरह की Requirement को पूरा करने के लिए कौनसी Class किस Namespace में Exist है, इसी विषय में कुछ Basic Discussion कर रहे हैं। जिसके आधार पर अन्य Classes व Namespaces को उपयोग में लेने के विषय में समझा जा सकता है।