我正在尝试使用DiskInfo
对象函数获取CD或DVD的可用磁盘空间,但从未成功过。我知道CDROM
是一个只读文件系统,当它上面的每个位都被认为是已分配的,因此当任何人试图使用普通函数获取磁盘上的可用空间时都会失败。我也尝试使用wmic api
但也失败了。
这是我的代码:
foreach (var drive in DriveInfo.GetDrives())
{
double freeSpace = drive.TotalFreeSpace;
double totalSpace = drive.TotalSize;
double percentFree = (freeSpace / totalSpace) * 100;
float num = (float)percentFree;
Console.WriteLine("Drive:{0} With {1} % free", drive.Name, num);
Console.WriteLine("Space Remaining:{0}", drive.AvailableFreeSpace);
Console.WriteLine("Percent Free Space:{0}", percentFree);
Console.WriteLine("Space used:{0}", drive.TotalSize);
Console.WriteLine("Type: {0}", drive.DriveType);
}
下面是我试过的任何c#代码都会返回的示例。
信:C - 总计:244095 - 免费:75851 - 类型:已解决 信:E - 总计:NULL- 免费:NULL- 类型:CDRom
在下面的@kettch
和@Craig Selbert
回复之后,我尝试使用GetDiskFreeSpaceEx
函数的另一种方式,但它可能不起作用,这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace IMAPIv2
{
public class Burn
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
ImageMaster _imageMaster;
public String burnData(String filesToBurn,String driverIndex,String volumeLabel,String boolColseMedia,String boolEject){
try
{
String[] sep = { "<!>" };
String[] files = filesToBurn.Split(sep, StringSplitOptions.RemoveEmptyEntries);
DriveInfo driver = getSelectedDriver(int.Parse(driverIndex));
Console.WriteLine(driver.Name);
Console.WriteLine(getDirSize(driver.Name));
ulong FreeBytesAvailable;
ulong TotalNumberOfBytes;
ulong TotalNumberOfFreeBytes;
bool success = GetDiskFreeSpaceEx(driver.Name, out FreeBytesAvailable, out TotalNumberOfBytes,
out TotalNumberOfFreeBytes);
if (!success)
throw new System.ComponentModel.Win32Exception();
Console.WriteLine("Free Bytes Available: {0,15:D}", FreeBytesAvailable);
Console.WriteLine("Total Number Of Bytes: {0,15:D}", TotalNumberOfBytes);
Console.WriteLine("Total Number Of FreeBytes: {0,15:D}", TotalNumberOfFreeBytes);
_imageMaster = new ImageMaster();
_imageMaster._recorders.SelectedIndex = Convert.ToInt32(driverIndex);
_imageMaster.LoadRecorder();
_imageMaster.totalFilesToBurn = addFilesToBurn(files);
_imageMaster.VolumeLabel = volumeLabel;
_imageMaster.LoadMedia();
_imageMaster.WriteImage(BurnVerificationLevel.Quick, Convert.ToBoolean(boolColseMedia), Convert.ToBoolean(boolEject));
}
catch (Exception e)
{
throw e;
}
String[] res = { "P:Finished" };
return res[0];
}
private long addFilesToBurn(String[] files)
{
long totalSize = 0;
for (int i = 0; i < files.Length; i++)
{
FileAttributes attr = File.GetAttributes(files[i]);
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
_imageMaster.Nodes.Add(new DirectoryNode(files[i]));
}
else
{
_imageMaster.Nodes.Add(new FileNode(files[i]));
totalSize += new FileNode(files[i]).SizeOnDisc;
}
}
return totalSize;
}
private DriveInfo getSelectedDriver(int driverindex)
{
DriveInfo[] df = DriveInfo.GetDrives();
DriveInfo selectedDriver = null;
int index = 0;
foreach ( DriveInfo d in df){
if (d.DriveType.Equals(DriveType.CDRom)){
if(index == driverindex){
selectedDriver= d;
break;
}
index++;
}
}
if (selectedDriver == null){
if (index == 0)
{
throw new InvalidOperationException("Please make sure you have a working optical drive to continue");
}
else
{
foreach (DriveInfo d in df)
{
if (d.DriveType.Equals(DriveType.CDRom))
{
selectedDriver = d;
break;
}
}
}
}
return selectedDriver;
}
private long getDirSize(String dir)
{
long s = 0;
DirectoryInfo df = new DirectoryInfo(dir);
foreach (FileInfo f in df.GetFiles())
{
s += f.Length;
}
foreach (DirectoryInfo d in df.GetDirectories())
{
s += getDirSize(d.FullName);
}
return s;
}
}
}
此代码的输出为:
E:\
296589891
Free Bytes Available: 0
Total Number Of Bytes: 309248
Total Number Of Free Bytes: 0
可用磁盘空间始终返回为零,但它不是如图所示
任何帮助将不胜感激,提前谢谢。