• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Programming / Retrieve logical drive info in C#.NET

Retrieve logical drive info in C#.NET

November 10, 2010 by Rana Ahsan 5 Comments

c# Tutorials

Curious programmers don’t bound themselves in any limit up to which scope they will spread their programming knowledge. They always interested in learning new things. I am one of them. I also like to do some things those aren’t inside my scope f professional work. To retrieve drive info, access windows service/processes, detect removable disk/cd-dvd etc are such kind of interests for programmers as well as to me too ๐Ÿ™‚ . From such interest, I have done some code and here, from that experience, i will try to share a very simple way with c# code examples to get our computer’s logical drive’s basic information. This is helpful on projects which need to determine drive’s blank space/capacity/file system type, detect/access removable disks etc.

In theย  first example, we will see how to get this information by utilizing MangementScope, ManagementObjectSearcher, ObjectQuery, ManagementObjectCollection, DirectoryInfo classes.

ManagementScope ms = new ManagementScope();
            ObjectQuery oq = new ObjectQuery("SELECT DeviceID, VolumeName FROM Win32_LogicalDisk");
            ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq);
            ManagementObjectCollection moc = mos.Get();

foreach (ManagementObject mo in moc)
            {
                DirectoryInfo newDI = new DirectoryInfo(System.IO.Path.Combine(mo["DeviceID"].ToString(), " "));
                newDI.Refresh();
                FileInfo[] files =  newDI.GetFiles();
               FileSystemInfo[] info = newDI.GetFileSystemInfos();
               //Now Access/Show/Return details information from "files" and "info" objects
}

The above code snippet will require using System.Management and System.IO name space to be included to get access to the classes mentioned. Here you will notice an interesting object instance of ‘ObjectQuery’ class. Yes, as you were thinking, we can write SQL like query to retrieve information from .net objects. The current query will retrieve all logical drives. But, suppose, if you want information from only removable disks, then you can use a ‘WHERE’ just like on standard SQL query like this

SELECT DeviceID, VolumeName FROM Win32_LogicalDisk WHERE DriveType=2

Here ‘DriveType’ is an property of ‘Win32_LogicalDisk’ class and 2 values stands for removable disks. You can see more details about them on microsoft’s msdn online.

If you don’t have to retrieve specific type drive and want information from all drives, then you can also use ‘DriveInfo’ class. See the following code sample:

DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
      Console.WriteLine("Drive {0}", d.Name);
      Console.WriteLine("File type: {0}", d.DriveType);
}

The above code snippet will show drive name and drive’s file type on a console application. As long you are on system.io namespace, you won’t need to add any other thing to get this work.

If you have any specific question regarding retrieval of drive info in c#, please ask them here in comments. I will try to answer as soon as I can. Happy coding ๐Ÿ™‚

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Programming Tagged With: .net, c#

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. manideep says

    March 24, 2013 at 3:25 am

    its nice and amazing .. i have a small doubt .. how to write queries in objectquery ?
    if i want to get the list of all posssible resolutions of any display device then what is the query i have to write .. please suggest me … Thanks in advance

    Reply
  2. Sergio Aranda says

    April 16, 2013 at 9:35 am

    Why I Obtain the next message: {“The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)”}?

    Reply
    • Md Ali Ahsan Rana says

      April 18, 2013 at 1:44 am

      Not exactly sure, but are you running the application as administrator? This stackoverflow thread may help your further: http://stackoverflow.com/questions/4657724/wmi-the-rpc-server-is-unavailable-exception-from-hresult-0x800706ba-throws

      Reply
  3. alex says

    April 30, 2015 at 11:09 pm

    How do I get only the floppy drives?
    Thanks in advance.

    Reply
    • Md Ali Ahsan Rana says

      May 1, 2015 at 12:08 am

      Not sure, but should work in similar way. Are you getting any error? I don’t have a floppy drive anymore and can’t test, sorry.

      Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Utilizing Config File In C#.NET Application
    Utilizing Config File In C#.NET Application
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Getting Started With UDP Programming in Java
    Getting Started With UDP Programming in Java
  • Generate HTTP Requests using c#
    Generate HTTP Requests using c#
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
    How To Use Hotkeys/Keyboard Events In WPF Application Using C#

Recent Posts

  • Building Auth With JWT โ€“ Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023