2016学习笔记(一)

来源:互联网 发布:推荐俄罗斯代购知乎 编辑:程序博客网 时间:2024/06/06 08:28

最近将自己这一年的资料进行整理,分享给大家!


1.U盘的driverinfo的属性。

using System;using System.IO;

class Test

{

    public static void Main()

    {

        DriveInfo[] allDrives = DriveInfo.GetDrives();

 

        foreach (DriveInfo d in allDrives)

        {

            Console.WriteLine("Drive {0}", d.Name);

            Console.WriteLine("  Drive type: {0}", d.DriveType);

            if (d.IsReady == true)//这里就是一个观察驱动是否准备好的,所以当设备要和PC进行通信时候,先要判断其是否已经准备好才能进行写号或者其他文件交流的功能。

            {

                Console.WriteLine("  Volume label: {0}", d.VolumeLabel);

                Console.WriteLine("  File system: {0}", d.DriveFormat);

                Console.WriteLine(

                    "  Available space to current user:{0, 15} bytes",

                    d.AvailableFreeSpace);

 

                Console.WriteLine(

                    "  Total available space:          {0, 15} bytes",

                    d.TotalFreeSpace);

 

                Console.WriteLine(

                    "  Total size of drive:            {0, 15} bytes ",

                    d.TotalSize);

            }

        }

    }

}/*

This code produces output similar to the following:

 

Drive A:\

  Drive type: Removable

Drive C:\

  Drive type: Fixed

  Volume label:

  File system: FAT32

  Available space to current user:     4770430976 bytes

  Total available space:               4770430976 bytes

  Total size of drive:                10731683840 bytes

Drive D:\

  Drive type: Fixed

  Volume label:

  File system: NTFS

  Available space to current user:    15114977280 bytes

  Total available space:              15114977280 bytes

  Total size of drive:                25958948864 bytes

Drive E:\

  Drive type: CDRom

 

The actual output of this code will vary based on machine and the permissions

granted to the user executing it.

*/

2.string[]数组里面是存放string型的值,List<string>是存放string类型的对象。

3.C#读取U盘序列号

private List<string> _serialNumber = new List<string>();

/// <summary>
/// 调用这个函数将本机所有U盘序列号存储到_serialNumber
/// </summary>
private void matchDriveLetterWithSerial()
{
    string[] diskArray;
    string driveNumber;
    var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
    foreach (ManagementObject dm in searcher.Get())
    {
        getValueInQuotes(dm["Dependent"].ToString());
        diskArray = getValueInQuotes(dm["Antecedent"].ToString()).Split(',');
        driveNumber = diskArray[0].Remove(06).Trim();
        var disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
        foreach (ManagementObject disk in disks.Get())
        {
            if (disk["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE" + driveNumber) & disk["InterfaceType"].ToString() == "USB")
            {
                _serialNumber.Add(parseSerialFromDeviceID(disk["PNPDeviceID"].ToString()));
            }
        }
    }
}
private static string parseSerialFromDeviceID(string deviceId)
{
    var splitDeviceId = deviceId.Split('\\');
    var arrayLen = splitDeviceId.Length - 1;
    var serialArray = splitDeviceId[arrayLen].Split('&');
    var serial = serialArray[0];
    return serial;
}

private static string getValueInQuotes(string inValue)
{
    var posFoundStart = inValue.IndexOf("\"");
    var posFoundEnd = inValue.IndexOf("\"", posFoundStart + 1);
    var parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd - posFoundStart) - 1);
    return parsedValue;
}


0 0
原创粉丝点击