zoukankan      html  css  js  c++  java
  • c#获取分区信息扩展

    using System;
    using System.Management;

    ...

    ManagementObject disk = new
    ManagementObject("win32_logicaldisk.deviceid="c:"");
    disk.Get();
    Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");
    Console.WriteLine("Logical Disk FreeSpace = " + disk["FreeSpace"] + "
    bytes");



    Drive Free Space

    There are several ways to get the drive free space:

    1. The interop way.

    using System.Runtime.InteropServices;

    [DllImport("kernel32.dll")]
    public static extern bool GetDiskFreeSpaceEx(
    string lpDirectoryName,
    out UInt64 lpFreeBytesAvailable,
    out UInt64 lpTotalNumberOfBytes,
    out UInt64 lpTotalNumberOfFreeBytes);

    ulong freeBytesAvailable = 0;
    ulong totalNumberOfBytes = 0;
    ulong totalNumberOfFreeBytes = 0;

    GetDiskFreeSpaceEx(
    "c:\\",
    out freeBytesAvailable,
    out totalNumberOfBytes,
    out totalNumberOfFreeBytes);

    2. The WMI Way

    We can use WMI by connecting to the “root\\cimv2” namespace and using the “Win32_LogicalDisk” class. Programming WMI isn’t pretty, but you can use the WMI extensions for VS ‘03 Server Explorer which makes it tolerable. Once installed, you’ll get a list of the management classes in server explorer and you can simply drag and drop the disk volume that you want to pull information from onto your application designer.

    You can then show free space on the volume with one line of code: MessageBox.Show(logicalDisk1.FreeSpace.ToString());

    using System;
    using System.Management;

    tatic void Main(string[] args)
    {
    WqlObjectQuery wmiquery = new WqlObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'");
    ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery);

    foreach (ManagementObject mobj in wmifind.Get())
    {
    Console.WriteLine("Description: " + mobj["Description"]);
    Console.WriteLine("File system: " + mobj["FileSystem"]);
    Console.WriteLine("Free disk space: " + mobj["FreeSpace"]);
    Console.WriteLine("Size: " + mobj["Size"]);
    }
    }

    3. The Whidbey Way

    System.IO.DriveInfo.AvailableFreeSpace
  • 相关阅读:
    852. Peak Index in a Mountain Array
    841. Keys and Rooms
    832. Flipping an Image
    821. Shortest Distance to a Character
    824. Goat Latin
    如何生成git的公钥和私钥
    学习笔记
    加快JavaScript加载和执行效率
    PO BO VO DTO POJO DAO概念及其作用
    jvm 垃圾回收区的形象说明
  • 原文地址:https://www.cnblogs.com/captain_ccc/p/1521775.html
Copyright © 2011-2022 走看看