DriveInfo类
DriveInfo类用于获取有关驱动器(如硬盘驱动器、软盘驱动器、CD-ROM 驱动器等)的信息。
// 获取所有逻辑驱动器的信息 Drive驱动
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
Console.WriteLine($"Drive: {drive.Name}"); // 驱动名称
Console.WriteLine($" Volume Label: {drive.VolumeLabel}"); // 卷标
Console.WriteLine($" Type: {drive.DriveType}"); // 驱动类型
Console.WriteLine($" Available Space: {drive.AvailableFreeSpace / 1024 / 1024/ 1024} bytes");// 可用空间
Console.WriteLine($" Total Size: {drive.TotalSize / 1024 / 1024 / 1024} bytes"); // 总空间
Console.WriteLine($" IsReady: {drive.IsReady}");// 磁盘是否准备好
Console.WriteLine($" Root Directory: {drive.RootDirectory.Name}");//盘符
}
DriveInfo info = new DriveInfo("D");
Console.WriteLine(info.AvailableFreeSpace/ 1024 / 1024 / 1024D);
Path类
Path类提供了用于处理文件和目录路径的静态方法。
// 目录===文件夹,路径
// DirectorySeparatorChar 目录分割符有两种:/ \,其中\默认的,
Console.WriteLine($"Path.DirectorySeparatorChar: '{Path.DirectorySeparatorChar}'");
// Alternate可选,可切换 分割符/
Console.WriteLine($"Path.AltDirectorySeparatorChar: '{Path.AltDirectorySeparatorChar}'");
// 多个路径之间分割符,是英文的分号;
Console.WriteLine($"Path.PathSeparator: '{Path.PathSeparator}'");
// 规律:方法带s结尾基本上集合 Invalid非法字符
var invalidChars = Path.GetInvalidPathChars();
string str = string.Empty;
foreach (char c in invalidChars)
{
str += c.ToString();
}
Console.WriteLine(str);
// Volume卷标,盘符,分割符英文:
Console.WriteLine($"Path.VolumeSeparatorChar: '{Path.VolumeSeparatorChar}'");
// 注意:最后一个英文点后面的才是后缀名(扩展名)
string goodFileName = @"C:\mydir\myfile.com.extension";
string result = Path.ChangeExtension(goodFileName, ".old");
Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'",
goodFileName, result);
// 拿扩展名,注意:取扩展名时,包含英文点
Console.WriteLine(Path.GetExtension(goodFileName)); // .extension
string[] pathArr = goodFileName.Split(new char[] { '.' });
string ext = pathArr[pathArr.Length - 1];
Console.WriteLine(ext);// 不带英文.
// 路径拼接,合并 Combine合并
// d:\archives\2001\media\images\a.jpg
string[] paths = { @"d:\archives", "2001", "media", "images", "a.jpg" };
string fullPath = Path.Combine(paths);
Console.WriteLine(fullPath);
// GetDirectoryName获取路径的目录部分
Console.WriteLine(Path.GetDirectoryName(goodFileName));
Console.WriteLine(Path.GetDirectoryName(fullPath));
// 取路径中的文件部分
Console.WriteLine(Path.GetFileName(fullPath));
Console.WriteLine(Path.GetFileName(@"D:\abc"));
Console.WriteLine(Path.GetFileNameWithoutExtension(fullPath)); // a
Console.WriteLine(Path.GetFullPath(fullPath));
char[] chars = Path.GetInvalidFileNameChars();
Console.WriteLine(string.Join("-", chars));
Console.WriteLine(Path.GetPathRoot(fullPath));
Console.WriteLine(Path.GetRandomFileName());
Console.WriteLine(Path.GetRandomFileName());
// 随机生成一个txt文件,要求:还上时间戳
string randomString = Path.GetRandomFileName();
string fileName = randomString.Split(new char[] { '.' })[0];
string tick = DateTime.Now.Ticks.ToString();
string fullFileName = $"{tick}-{fileName}.txt";
Console.WriteLine(fullFileName);
Console.WriteLine($"{tick}-{Path.ChangeExtension(randomString, ".txt")}");
// C:\Users\XXXX\AppData\Local\Temp
Console.WriteLine(Path.GetTempFileName());
Console.WriteLine(Path.GetTempFileName());
标签:1024,Console,string,DriveInfo,WriteLine,Path
From: https://www.cnblogs.com/dbsdb/p/18364911