Hi Friends,
Let see how can you get the USB drive information using the C# code. I mean the code which shows us if the Removable drive is inserted in computer.
If you want to trigger some program, you have to implement this code in window service and check for the condition in loop.
For the console program the below code shows you how can you get the removable drive and get the other information related to that drive.
You will see the code like below
Remember- d.IsReady is must, other wise it will throw error when it will try to get the information of the CD-Rom and found CD-Rom is empty mean we have not inserted any CD or DVD, in this situation CD-Rom is not ready so by applying the proper LINQ we have removed the not ready drive from the all drive collection.
Happy Reading and knowledge sharing.. !! :)
Let see how can you get the USB drive information using the C# code. I mean the code which shows us if the Removable drive is inserted in computer.
If you want to trigger some program, you have to implement this code in window service and check for the condition in loop.
For the console program the below code shows you how can you get the removable drive and get the other information related to that drive.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace DetectRemovableDrive { class Program { static void Main(string[] args) { // for all the ready drives IEnumerableCreate a console program the paste the following code in Program.cs file. And compile the code.a= DriveInfo.GetDrives().Where(d => d.IsReady); // if you want to get only Removable drives use the below two lines.. //IEnumerable a = DriveInfo.GetDrives().Where(d => d.IsReady //&& d.DriveType == System.IO.DriveType.Removable); Console.WriteLine("Total {0} ready drives found.!!\n", a.Count ()); foreach(DriveInfo temp in a) { Console.WriteLine("\tDrive {0} has the label \"{1}\"", temp.Name, temp.VolumeLabel==""?"Local Disk":temp.VolumeLabel); Console.WriteLine("\t\tTotal size {0} bytes, free is {1} bytes.\n", temp.TotalSize, temp.TotalFreeSpace); } Console.ReadKey(); } } }
You will see the code like below
Total 5 ready drives found.!! Drive C:\ has the label "Local Disk" Total size 104752738304 bytes, free is 27952164864 bytes. Drive D:\ has the label "Local Disk" Total size 364670611456 bytes, free is 207251906560 bytes. Drive G:\ has the label "Deepak" Total size 214748360704 bytes, free is 214185193472 bytes. Drive H:\ has the label "WD Unlocker" Total size 7507968 bytes, free is 0 bytes. Drive I:\ has the label "My Passport" Total size 1000169533440 bytes, free is 836563316736 bytes.
Remember- d.IsReady is must, other wise it will throw error when it will try to get the information of the CD-Rom and found CD-Rom is empty mean we have not inserted any CD or DVD, in this situation CD-Rom is not ready so by applying the proper LINQ we have removed the not ready drive from the all drive collection.
Happy Reading and knowledge sharing.. !! :)
No comments:
Post a Comment