首页 > 代码库 > 磁盘操作

磁盘操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Management;

namespace DotNet.MYClass.公共广展函数
{
class 磁盘
{

#region 磁盘驱动器 操作
/// </summary>
/// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 </param>
public static long GetHardDiskSpace(string str_HardDiskName)
{
long totalSize = new long();
str_HardDiskName = str_HardDiskName + ":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == str_HardDiskName)
{
totalSize = drive.TotalSize;
}
}
return totalSize;
}

/// <summary>
/// 获取指定驱动器的剩余空间总大小(单位为B)
/// </summary>
/// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 </param>
public static long GetHardDiskFreeSpace(string str_HardDiskName)
{
long freeSpace = new long();
str_HardDiskName = str_HardDiskName + ":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == str_HardDiskName)
{
freeSpace = drive.TotalFreeSpace;
}
}
return freeSpace;
}
//获取 磁盘容量
public static string GetHardDiskFreeSpaceCH(long freeSpace)
{
string driverSpace = "";
if (freeSpace > 1024 * 1024 * 1024)
{
driverSpace = freeSpace / 1024 / 1024 / 1024 + "G";
}
else if (freeSpace > 1024 * 1024)
{
driverSpace = freeSpace / 1024 / 1024 + "M";
}
return driverSpace;
}

/// <summary>
/// 获取指定驱动器的剩余空间总大小(单位为B)
/// </summary>
public static string GetHardDiskMaxSpace()
{
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
long[] spaceS = new long[drives.Length];
//初始化磁盘大小
for (int i = 0; i < drives.Length; i++)
{
if (drives[i].DriveType == System.IO.DriveType.Fixed)
{
spaceS[i] = drives[i].TotalFreeSpace;
}
}

//取出最大的一个
long maxSpace = spaceS[0];
string maxDriver = drives[0].Name;
for (int i = 1; i < spaceS.Length; i++)
{
if (maxSpace < spaceS[i])
{
maxSpace = spaceS[i];
maxDriver = drives[i].Name;
}
}
return maxDriver + "@" + maxSpace;
}

/// <summary>
/// 获取指定驱动器的剩余空间总大小(单位为B)
/// </summary>
public static string GetHardDiskSMA()
{
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
string smaPath = "";
foreach (System.IO.DriveInfo drive in drives)
{
try
{
if (!drive.Name.Equals("A:\\") && drive.DriveType == System.IO.DriveType.Removable && drive.IsReady && drive.VolumeLabel.ToUpper().Equals("SMA"))
{
smaPath = drive.Name;
break;
}
else if (drive.VolumeLabel.ToUpper().Equals("SMA"))
{
smaPath = drive.Name;
break;
}
}
catch (Exception)
{
}
}
return smaPath;
}
#endregion
}

public class Disk
{
static Semaphore sem = new Semaphore(1, 1);

#region 全局
//使用存储磁盘列表
public static List<string> DiskList = new List<string>();
//所有磁盘
public static Dictionary<string, string> DiskArray = new Dictionary<string, string>();
//当前使用的磁盘
private static string _employDiak = "D:";

public static string EmployDiak
{
get
{
sem.WaitOne();
//APPLoger.Writer("Disk sem.WaitOne()");
string str = Disk._employDiak;
sem.Release();
//APPLoger.Writer("Disk sem.Release()");
return str;
}
set
{
sem.WaitOne();
//APPLoger.Writer("Disk sem.WaitOne()");
Disk._employDiak = value;
sem.Release();
//APPLoger.Writer("Disk sem.Release()");
}
}

public static List<string> GetDiskListS()
{
List<string> list = new List<string>();
try
{
SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);

foreach (ManagementObject disk in searcher.Get())
{
//盘符
string DiskStr = disk["name"].ToString();
//驱动器类型
string DriveType;
try
{
DriveType = disk["DriveType"].ToString();
switch (DriveType)
{
case "2"://diskE.DiskType = "可移动磁盘";
case "3"://diskE.DiskType = "硬盘";
case "4"://diskE.DiskType = "网络驱动器";
case "6"://diskE.DiskType = "内存磁盘";
if (DiskStr != "C:")
{
list.Add(DiskStr);
}
break;
}
}
catch (Exception ex)
{

}

}
}
catch (Exception ex)
{

}
return list;
}

/// <summary>
/// 获取所有磁盘
/// </summary>
public static void DISKINI()
{
try
{
DiskArray = new Dictionary<string, string>();
SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);

foreach (ManagementObject disk in searcher.Get())
{
//盘符
string DiskStr = disk["name"].ToString();
//驱动器类型
string DriveType;
try
{
DriveType = disk["DriveType"].ToString();
switch (DriveType)
{
case "2"://diskE.DiskType = "可移动磁盘";
case "3"://diskE.DiskType = "硬盘";
case "4"://diskE.DiskType = "网络驱动器";
case "6"://diskE.DiskType = "内存磁盘";
DiskArray.Add(DiskStr, DiskStr);
break;
}
}
catch (Exception ex)
{

}
}

}
catch (Exception ex)
{
}
}
#endregion

//////////////////////////////////////////////////////////////////////////////////

#region
bool GetDiskBool = false;
Thread GetDiskThread = null;

//启动定时获取列表空间大小
public void StartGetDisk()
{
try
{
GetDiskThread = new Thread(new ThreadStart(GetDiskList));
lock (this)
{
GetDiskBool = true;
}
GetDiskThread.Start();
}
catch (Exception ex)
{

}
}

public void StopGetDisk()
{
try
{
lock (this)
{
GetDiskBool = true;
}
GetDiskThread.Join(1000);
if (GetDiskThread != null)
{
GetDiskThread.Abort();
}
}
catch (Exception ex)
{

}
}

/////////////////////////////////////////////////////////////////////
void GetDiskList()
{
do
{
try
{
if (DiskList != null && DiskList.Count > 0)
{
string str = EmployDiak;
int isDias = -1;//-1默认值

//当前磁盘是否存在于存储磁盘列表中
foreach (string Diskstr in DiskList)
{
if (Diskstr.Equals(str))
{
isDias = 1;//是
}
}
if (isDias == -1)//上面处理完还是-1表示不存在于列表中
{
//选择中列表第一个磁盘
str = DiskList[0];
isDias = 1;
}
int index = 0;
//查找可用的当前存储磁盘
for (int i = 0; i < DiskList.Count; i++)
{
string Diskstr = DiskList[i];
if (DiskArray.ContainsKey(Diskstr))//是否真实存在的磁盘
{
//获取指定磁盘的信息
long DiskSurplusSize = GetHardDiskFreeSpace(Diskstr);
if (DiskSurplusSize > 0)
{
long DiseSize_G = DiskSurplusSize / 1024 / 1024 / 1024;
if (Diskstr.Equals(str))//当前使用的磁盘不足3G
{
if (DiseSize_G <= 3)
{
isDias = 2;//不足更换
index++;
}
else //空间足
{
break;
}
}
else //不是当前磁盘
{
if (DiseSize_G <= 3)
{
index++;
}
else
{
if (isDias == 2)
{
str = Diskstr;
isDias = 3;
}
}
}
}
}
}
if (index == DiskList.Count)
{
EmployDiak = "";
}
else
{
EmployDiak = str;
}
}
}
catch (Exception ex)
{

}
if (GetDiskBool)
{
Thread.Sleep(10000);
}
} while (GetDiskBool);
}

/// <summary>
/// 获取指定驱动器的剩余空间总大小(单位为B)
/// </summary>
/// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 </param>
/// <returns> </returns>
long GetHardDiskFreeSpace(string str_HardDiskName)
{
try
{
long freeSpace = new long();
str_HardDiskName = str_HardDiskName + "\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == str_HardDiskName)
{
freeSpace = drive.TotalFreeSpace;
return freeSpace;
}
}
return freeSpace;
}
catch (Exception ex)
{

return 0;
}
}
#endregion
}
}

磁盘操作