Found 34486 Articles for Programming

Get all drives in C#

Arjun Thakur
Updated on 22-Jun-2020 14:31:43

550 Views

Firstly, use GetDrives to get the name of all the drives −var drv = DriveInfo.GetDrives();Loop through to get the name of all the drives on the system −foreach (DriveInfo dInfo in drv) {    Console.WriteLine(dInfo.Name); }Let us see the complete code −Example Live Demousing System; using System.Linq; using System.IO; public class Demo {    public static void Main() {       var drv = DriveInfo.GetDrives();       foreach (DriveInfo dInfo in drv) {          Console.WriteLine(dInfo.Name);       }    } }Output/etc/resolv.conf /etc/hostname /etc/hosts /run/secrets /home/cg/rootNote: The result will vary on different Operating Systems. The above ... Read More

Get the drive format in C#

Chandu yadav
Updated on 06-Apr-2020 11:00:06

201 Views

Use the DriveFormat property to get the drive format in C#.Set the drive for which you want to display the format −DriveInfo dInfo = new DriveInfo("C");Now, use DriveFormat to get the drive format −dInfo.DriveFormatThe drive formats for a windows system can be NTFS or FAT32.Here is the complete code −Exampleusing System; using System.Linq; using System.IO; public class Demo {    public static void Main() {       DriveInfo dInfo = new DriveInfo("C");       Console.WriteLine("Drive Format = "+dInfo.DriveFormat);    } }OutputThe following is the output −Drive Format = NTFS

C# Program to get the name of root directory

George John
Updated on 22-Jun-2020 14:33:02

507 Views

Use the RootDirectory property to get the name of the root directory.Firstly, use DriveInfo to set the drive for which you want the root directory −DriveInfo dInfo = new DriveInfo("C");Now, the RootDirectory gives you the result −dInfo.RootDirectoryExampleHere is the complete code −using System; using System.Linq; using System.IO; public class Demo {    public static void Main() {       DriveInfo dInfo = new DriveInfo("C");       Console.WriteLine("Root Directory: "+dInfo.RootDirectory);    } }OutputThe following is the output −C:\

C# Program to get the name of the drive

Ankith Reddy
Updated on 22-Jun-2020 14:33:38

146 Views

Set the drive for which you want to display the name −DriveInfo info = new DriveInfo("C");Now, to get the drive name, use the Name property −info.NameHere is the complete code with output −Exampleusing System; using System.Linq; using System.IO; public class Demo {    public static void Main() {       DriveInfo info = new DriveInfo("C");       Console.WriteLine(“Drive: “+info.Name);    } }OutputThe following is the output −D:/

C# Program to get free space in a drive

Arjun Thakur
Updated on 06-Apr-2020 11:01:50

549 Views

To get the free space in a drive, use the AvailableFreeSpace and TotalFreeSpace properties in C#.Firstly, set the name of the drive using DriveInfo −DriveInfo dInfo = new DriveInfo("D");Let’s say, you need to find the available space for D drive −Exampleusing System; using System.Linq; using System.IO; public class Demo {    public static void Main() {       DriveInfo dInfo = new DriveInfo("D");       // Get free space       Console.WriteLine(dInfo.AvailableFreeSpace);       // get total free space       Console.WriteLine(dInfo.TotalFreeSpace);    } }OutputThe following is the output showing the free space available in D drive−722243567912 722243567912

C# Program to display the last three elements from a list in reverse order

Chandu yadav
Updated on 22-Jun-2020 14:19:58

1K+ Views

To display the last three elements from a list, use the Take() method. For reversing it, use the Reverse() method.Firstly, declare a list and add elements to it −List myList = new List(); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); myList.Add("Four");Now, use the Take() method with Reverse() to display the last three elements from the list in reverse order −myList.Reverse().Take(3);The following is the code −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       List myList = new List();       myList.Add("One");       myList.Add("Two");       myList.Add("Three");       ... Read More

C# orderby Keyword

George John
Updated on 22-Jun-2020 14:26:38

134 Views

Use the orderby leyword to sort a list in ascending or descending order.The following is the list −List myList = new List(); myList.Add("truck"); myList.Add("bus"); myList.Add("bicycle"); myList.Add("motorbike");Now let us sort the list in descending order −myLen = from element in myList orderby element.Length descending select element;Here is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List myList = new List();       myList.Add("truck");       myList.Add("bus");       myList.Add("bicycle");       myList.Add("motorbike");       var myLen = from element in myList     ... Read More

KeyNotFoundException in C#

Chandu yadav
Updated on 22-Jun-2020 14:27:28

439 Views

The KeyNotFoundException is thrown when a key you are finding is not available in the Dictionary collection.Let us see an example −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       try {          var dict = new Dictionary() {             {"TV", "Electronics"},             {"Laptop", "Computers"},          };          Console.WriteLine(dict["Pen Drive"]);       }       catch (Exception e) {          Console.WriteLine(e);       }    } ... Read More

Sort KeyValuePairs in C#

Arjun Thakur
Updated on 22-Jun-2020 14:28:04

600 Views

Use the Sort method to sort the KeyValuePairs collection.Firstly, set the collection −var myList = new List(); // adding elements myList.Add(new KeyValuePair(1, 20)); myList.Add(new KeyValuePair(2, 15)); myList.Add(new KeyValuePair(3, 35)); myList.Add(new KeyValuePair(4, 50)); myList.Add(new KeyValuePair(5, 25));To sort, use the Sort() method. With that, we have used the CompareTo() method to compare values −myList.Sort((x, y) => (y.Value.CompareTo(x.Value)));The following is the complete code −Example Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       var myList = new List();       // adding elements       myList.Add(new KeyValuePair(1, 20));       myList.Add(new KeyValuePair(2, 15)); ... Read More

C# Program to find the smallest element from an array

Ankith Reddy
Updated on 22-Jun-2020 14:14:43

2K+ Views

Declare an array −int[] arr = { 5, 9, 2, 7 };Now to get the smallest element from an array, use the Min() method −arr.Min());Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 5, 9, 2, 7 };       Console.WriteLine(arr.Min());    } }Output2

Advertisements