Hanson.Common.FileUtils 2.0.0

dotnet add package Hanson.Common.FileUtils --version 2.0.0                
NuGet\Install-Package Hanson.Common.FileUtils -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hanson.Common.FileUtils" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hanson.Common.FileUtils --version 2.0.0                
#r "nuget: Hanson.Common.FileUtils, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hanson.Common.FileUtils as a Cake Addin
#addin nuget:?package=Hanson.Common.FileUtils&version=2.0.0

// Install Hanson.Common.FileUtils as a Cake Tool
#tool nuget:?package=Hanson.Common.FileUtils&version=2.0.0                

README

Hanson.Common.FileUtils

主要功能

  1. 提供基本檔案功能
    • 唯讀方式讀取檔案內容
    • 讀取指定目錄下所有檔案完整路徑 (含子目錄中的檔案路徑)
    • 複製指定目錄下所有資料夾內容及檔案
  2. 計算檔案雜湊碼
    • MD5
    • SHA1
    • SHA256
    • SHA384
    • SHA512
  3. 提供 PList 資料存放功能
    • 寫入 PList 紀錄
    • 讀取 PList 紀錄
    • 刪除 PList 紀錄
  4. 提供檔案壓縮相關功能
    • zip
    • unzip
  5. 提供建立檔案還原點功能
    • 建立檔案還原點
    • 執行檔案還原
    • 清除還原點資料
  6. 提供檔案處理功能
    • 搬移檔案
    • 複製檔案
    • 刪除檔案

前置條件

  • 開發環境需具備 .Net 8.0
  • 運行於 Windows Platform (x64)
  • 運行於 Linux Platform (x64)
  • 運行於 Raspberry PI Platform (x64)

安裝方式

授權

此專案採用的 License為 Apache-2.0

使用範例

命名空間

using Hanson.Common.Utils;
  • 基本檔案功能範例
private void Sample()
{

    string src = @"C:\temp";
    string file = "test.txt";

    // 唯讀方式取得檔案內容
    string content = FileUtils.ReadFileContent(Path.Combine(src, file));
    Console.WriteLine($"唯讀方式取得檔案內容: {content}");

    // 讀取指定目錄下所有檔案完整路徑
    string[] fileNames = FileUtils.ReadFilePathRecursively(src);
    Console.WriteLine($"唯讀方式取得檔案內容: {string.Join(",", fileNames)}");

    // 複製指定目錄下所有資料夾內容及檔案
    FileUtils.CopyFilesRecursively(src,dest);
}
  • 檔案雜湊碼範例
private void Sample()
{
    //檔案來源
    string src = @"C:\temp\test.txt";

    string hashMD5 = FileUtils.CalculateMD5(src);
    Console.WriteLine($"MD5 檔案雜湊碼: {hashMD5}");

    string hashSHA1 = FileUtils.CalculateSHA1(src);
    Console.WriteLine($"SHA1 檔案雜湊碼: {hashSHA1}");

    string hashSHA256 = FileUtils.CalculateSHA256(src);
    Console.WriteLine($"SHA256 檔案雜湊碼: {hashSHA256}");

    string hashSHA384 = FileUtils.CalculateSHA384(src);
    Console.WriteLine($"SHA384 檔案雜湊碼: {hashSHA384}");

    string hashSHA512 = FileUtils.CalculateSHA512(src);
    Console.WriteLine($"SHA512 檔案雜湊碼: {hashSHA512}");
}
  • PList 資料存放範例
private static void Sample()
{
    string key = "key";
    string value = "value";

    // 寫入 PList 資料
    bool status = FileUtils.WritePList(key, value);
    Console.WriteLine($"PList 寫入狀態: {status}");

    // 讀取 key 的 PList 資料
    PList data = FileUtils.ReadPList(key);
    Console.WriteLine($"PList 讀出內容: key:{data.Key}, value:{data.Value}, time:{data.Timestamp.ToDateTime()}");

    // 讀取所有 PList 資料
    PList[] datas = FileUtils.ReadPLists();
    foreach (var item in datas)
    {
        Console.WriteLine($"PList 讀出內容: key: {item.Key} , value: {item.Value} , time: {item.Timestamp.ToDateTime()}");
    }

    // 刪除 key 的 PList 資料
    int count = FileUtils.RemovePList(key);
    Console.WriteLine($"刪除 PList 內容的數量: {count}");
}
  • 檔案壓縮範例
private void Sample()
{
    //建立 ICompress
    ICompress compress = CompressFactory.CreateCompress();

    // 壓縮 ZIP
    string zipSrc = @"C:\temp";
    string zpiDest = @"D:\temp\test.zip";
    compress.Compress(zipSrc, zpiDest);

    // 解壓縮 ZIP
    string unzipSrc = @"D:\temp\test.zip";
    string unzipDest = @"C:\temp";
    compress.UnCompress(unzipSrc, unzipDest);
}
  • 檔案還原點範例
private void Sample()
{
    //放置暫存檔的位置
    string tempSrc = @"c:\temp";

    //檔案主要覆蓋位置
    string target = @"c:\target";

    //更新檔案來源
    string src = @"c:\src";

    FileRestorePoint checkPoint = new FileRestorePoint(tempSrc);
    try
    {
        Console.WriteLine($"開始執行檔案備份:{target}");
        checkPoint.Establish(target);
        Console.WriteLine($"檔案備份完成");

        // 還原點設定好後,開始執行的複製作業,若複製失敗會拋出例外訊息
        Console.WriteLine($"開始複製檔案,來源位址:{src}、目標位址:{target}");
        FileUtils.CopyFilesRecursively(src, target);
        Console.WriteLine($"檔案複製完成");
    }
    catch
    {
        Console.WriteLine($"檔案複製失敗");
        try
        {
            checkPoint.Restore();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    finally
    {
        checkPoint.Drop();
        checkPoint.Dispose();
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Hanson.Common.FileUtils:

Package Downloads
Hanson.AutoUpdater

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 92 11/30/2024
1.1.0 104 11/4/2024
1.0.0 195 2/19/2024