DriveInfo類,Path類

海域發表於2024-08-17

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());

相關文章