📄 devicememoryinfo.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace GetStorageCard
{
public class DeviceMemoryInfo
{
public static bool HasStorageCardPresent
{
get
{
return DeviceMemoryInfo.IsStorageCard();
}
}
private static long freeBytesAvailable = 0;
public static long FreeBytesAvailable
{
get
{
DeviceMemoryInfo.GetStoreageSize();
return freeBytesAvailable;
}
}
private static long totalBytes = 0;
public static long TotalBytes
{
get
{
DeviceMemoryInfo.GetStoreageSize();
return totalBytes;
}
}
private static long totalFreeBytes = 0;
public static long TotalFreeBytes
{
get
{
DeviceMemoryInfo.GetStoreageSize();
return totalFreeBytes;
}
}
static string directoryName = string.Empty;
/// <summary>
/// Set directory Name before get Memory status,default is windows directory
/// </summary>
public static string DirectoryName
{
get
{
return directoryName;
}
set
{
directoryName = value;
}
}
[DllImport("coredll")]
private static extern bool GetDiskFreeSpaceEx(string directoryName, ref long freeBytesAvailable, ref long totalBytes, ref long totalFreeBytes);
private static void GetStoreageSize()
{
DiskFreeSpace result = new DiskFreeSpace();
if (string.IsNullOrEmpty(directoryName))
{
directoryName = @"\Windows";
}
if (!GetDiskFreeSpaceEx(directoryName, ref result.FreeBytesAvailable, ref result.TotalBytes, ref result.TotalFreeBytes))
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Error retrieving free disk space");
}
else
{
freeBytesAvailable = result.FreeBytesAvailable;
totalBytes = result.TotalBytes;
totalFreeBytes = result.TotalFreeBytes;
}
}
private struct DiskFreeSpace
{
/// <summary>
/// The total number of free bytes on the disk that are available to the user associated with the calling thread.
/// </summary>
public long FreeBytesAvailable;
/// <summary>
/// The total number of bytes on the disk that are available to the user associated with the calling thread.
/// </summary>
public long TotalBytes;
/// <summary>
/// The total number of free bytes on the disk.
/// </summary>
public long TotalFreeBytes;
}
private static bool IsStorageCard()
{
bool hasStorageCard = false;
DirectoryInfo storeageCard = new DirectoryInfo(@"\");
foreach (DirectoryInfo directory in storeageCard.GetDirectories())
{
if (directory.Attributes == (FileAttributes.Temporary | FileAttributes.Directory))
{
hasStorageCard = true;
break;
}
else
{
return hasStorageCard = false;
}
}
return hasStorageCard;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -