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(0, 6).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;
}
- 2016学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- 学习笔记(一)
- ActiveMQ 简述
- mysql多实例安装说明
- Git8--添加远程库
- 操作系统对内存管理
- 使用boost::function 实现一个 单例线程
- 2016学习笔记(一)
- 用selenium打开谷歌浏览器提示 chromedriver.exe停止运行
- 项目中引入log4j
- App性能之优化
- 使用Studio在genymotion模拟器上运行项目时提示错误:installation failed with message INSTALL_FAILED_CPU_ABI_INCOMPATIBL
- 随机数
- 求两数的最大公因数
- 鼠标操作
- Erlang开发笔记之数据类型