鉴于下面的每个输入,我想在该位置获得可用空间.就像是
long GetFreeSpace(string path)
Run Code Online (Sandbox Code Playgroud)
输入:
c:
c:\
c:\temp
\\server
\\server\C\storage
Run Code Online (Sandbox Code Playgroud)
小智 57
这对我有用......
using System.IO;
private long GetTotalFreeSpace(string driveName)
{
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && drive.Name == driveName)
{
return drive.TotalFreeSpace;
}
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
祝好运!
Ric*_*dOD 39
DriveInfo将帮助您解决其中的一些问题(但它不适用于UNC路径),但我认为您需要使用GetDiskFreeSpaceEx.您可以使用WMI实现某些功能.GetDiskFreeSpaceEx看起来是你最好的选择.
您可能需要清理路径才能使其正常工作.
sas*_*gud 36
使用GetDiskFreeSpaceEx
来自RichardOD的链接的工作代码段.
// Pinvoke for API function
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
public static bool DriveFreeBytes(string folderName, out ulong freespace)
{
freespace = 0;
if (string.IsNullOrEmpty(folderName))
{
throw new ArgumentNullException("folderName");
}
if (!folderName.EndsWith("\\"))
{
folderName += '\\';
}
ulong free = 0, dummy1 = 0, dummy2 = 0;
if (GetDiskFreeSpaceEx(folderName, out free, out dummy1, out dummy2))
{
freespace = free;
return true;
}
else
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
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)
{
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.
*/
Run Code Online (Sandbox Code Playgroud)
这是 @sasha_gud 答案的重构和简化版本:
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
public static ulong GetDiskFreeSpace(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
}
ulong dummy = 0;
if (!GetDiskFreeSpaceEx(path, out ulong freeSpace, out dummy, out dummy))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return freeSpace;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
120681 次 |
最近记录: |