C#如何创建或解析NTFS文件系统的Hard Link硬链接

来源:编程学习作者:仓本头衔:网络博主
导读:本期聚焦于小伙伴创作的《C#如何创建或解析NTFS文件系统的Hard Link硬链接》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《C#如何创建或解析NTFS文件系统的Hard Link硬链接》有用,将其分享出去将是对创作者最好的鼓励。

NTFS文件系统的硬链接是同一个文件数据块的多个目录项引用,所有硬链接路径地位平等,删除其中一个不会影响其他路径对文件的访问,直到最后一个硬链接被删除时文件数据才会被真正清除。在C#中实现硬链接的创建和解析,需要借助Windows提供的底层API完成操作。

C#如何创建或解析NTFS文件系统的Hard Link硬链接

C#创建NTFS硬链接

Windows系统提供了CreateHardLink函数用于创建硬链接,该函数位于kernel32.dll中,我们需要在C#中通过平台调用服务(P/Invoke)声明这个函数,再封装成可调用的方法。

声明API函数

首先需要在C#代码中声明CreateHardLink的外部函数,指定对应的DLL和调用规则:

using System;
using System.Runtime.InteropServices;

public class HardLinkHelper
{
    // 声明CreateHardLink函数,返回bool表示创建是否成功
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern bool CreateHardLink(
        string lpFileName,       // 要创建的硬链接路径
        string lpExistingFileName, // 已存在的目标文件路径
        IntPtr lpSecurityAttributes // 安全属性,一般传IntPtr.Zero即可
    );
}

封装创建方法

基于声明的API函数,我们可以封装一个通用的创建硬链接方法,同时处理错误信息的获取:

using System;
using System.Runtime.InteropServices;

public class HardLinkHelper
{
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern bool CreateHardLink(
        string lpFileName,
        string lpExistingFileName,
        IntPtr lpSecurityAttributes
    );

    /// <summary>
    /// 创建NTFS硬链接
    /// </summary>
    /// <param name="hardLinkPath">硬链接的完整路径</param>
    /// <param name="targetFilePath">目标文件的完整路径</param>
    /// <returns>创建成功返回true,失败返回false</returns>
    public static bool CreateHardLinkFile(string hardLinkPath, string targetFilePath)
    {
        // 调用系统API创建硬链接
        bool result = CreateHardLink(hardLinkPath, targetFilePath, IntPtr.Zero);
        if (!result)
        {
            // 获取错误码,方便排查问题
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine($"创建硬链接失败,错误码:{errorCode}");
        }
        return result;
    }
}

调用示例

使用封装好的方法创建硬链接的示例代码如下:

class Program
{
    static void Main(string[] args)
    {
        string targetFile = @"C:testoriginal.txt";
        string hardLinkFile = @"C:testhardlink.txt";
        // 创建硬链接
        bool success = HardLinkHelper.CreateHardLinkFile(hardLinkFile, targetFile);
        if (success)
        {
            Console.WriteLine("硬链接创建成功");
        }
        else
        {
            Console.WriteLine("硬链接创建失败");
        }
    }
}

C#解析NTFS硬链接

解析硬链接主要是判断一个文件是否存在其他硬链接路径,以及获取所有指向同一数据块的硬链接路径。Windows提供了FindFirstFileNameWFindNextFileNameW函数来实现这个功能。

声明解析相关API

首先声明解析硬链接需要用到的两个API函数:

using System;
using System.Runtime.InteropServices;
using System.Text;

public class HardLinkHelper
{
    // 查找第一个硬链接名称
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern IntPtr FindFirstFileNameW(
        string lpFileName,
        uint dwFlags,
        ref uint stringLength,
        StringBuilder fileName
    );

    // 查找下一个硬链接名称
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern bool FindNextFileNameW(
        IntPtr findHandle,
        ref uint stringLength,
        StringBuilder fileName
    );

    // 关闭查找句柄
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool FindClose(IntPtr findHandle);
}

封装解析方法

封装获取文件所有硬链接路径的方法:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections.Generic;

public class HardLinkHelper
{
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern IntPtr FindFirstFileNameW(
        string lpFileName,
        uint dwFlags,
        ref uint stringLength,
        StringBuilder fileName
    );

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern bool FindNextFileNameW(
        IntPtr findHandle,
        ref uint stringLength,
        StringBuilder fileName
    );

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool FindClose(IntPtr findHandle);

    /// <summary>
    /// 获取文件的所有硬链接路径
    /// </summary>
    /// <param name="filePath">要查询的文件完整路径</param>
    /// <returns>所有硬链接路径的列表</returns>
    public static List<string> GetAllHardLinks(string filePath)
    {
        List<string> hardLinks = new List<string>();
        uint stringLength = 1024;
        StringBuilder fileName = new StringBuilder((int)stringLength);
        // 查找第一个硬链接
        IntPtr findHandle = FindFirstFileNameW(filePath, 0, ref stringLength, fileName);
        if (findHandle != IntPtr.Zero && findHandle.ToInt64() != -1)
        {
            // 添加找到的第一个路径
            hardLinks.Add(fileName.ToString());
            // 循环查找后续硬链接
            while (FindNextFileNameW(findHandle, ref stringLength, fileName))
            {
                hardLinks.Add(fileName.ToString());
            }
            // 关闭查找句柄
            FindClose(findHandle);
        }
        return hardLinks;
    }
}

解析调用示例

调用解析方法获取文件所有硬链接的示例:

class Program
{
    static void Main(string[] args)
    {
        string targetFile = @"C:testoriginal.txt";
        List<string> allHardLinks = HardLinkHelper.GetAllHardLinks(targetFile);
        Console.WriteLine($"文件 {targetFile} 的所有硬链接路径:");
        foreach (string link in allHardLinks)
        {
            Console.WriteLine(link);
        }
    }
}

注意事项

  • 硬链接仅支持NTFS文件系统,在FAT32、exFAT等文件系统中无法使用。
  • 硬链接不能跨卷创建,即目标文件和硬链接必须位于同一个磁盘分区。
  • 不能为目录创建硬链接,Windows系统仅支持文件的硬链接操作。
  • 调用API时如果失败,可以通过Marshal.GetLastWin32Error()获取错误码,根据错误码排查权限不足、目标文件不存在等问题。

C#NTFSHard_Link硬链接修改时间:2026-07-19 11:33:30

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。