c# - 列出文件的硬链接(hard link)(在 C# 中)

标签 c# hardlink junction

我想写一个程序,用硬链接(hard link)显示另一个驱动器的文件。

我想让两个硬链接(hard link)在文件名和其他方面保持一致,所以我必须获得一个函数/方法,我可以在其中列出文件的所有当前硬链接(hard link)。

例如:

我有一个文件 C:\file.txt 和第二个到 D:\file.txt 的硬链接(hard link)。

然后我将 D:\file.txt 重命名为 D:\file_new.txt

我现在也希望能够重命名 C 盘上的硬链接(hard link)。

所以我需要一个为 D:\file_new.txt 返回有以下硬链接(hard link)的函数:

C:\file.txt
D:\file_new.txt

然后我可以重命名 C:\ 上的硬链接(hard link)也可以得到 D:\file_new.txt

所以我需要获取物理文件的所有硬链接(hard link)。 或者:文件的所有硬链接(hard link)都使用硬链接(hard link)寻址。

希望有人能帮忙!

编辑:

Oliver 注意到硬链接(hard link)不能在不同的磁盘上使用。谢谢...所以我将问题扩展为:我需要什么?连接点?符号链接(symbolic link)?它不仅可以处理文件夹,还应该处理文件!

最佳答案

以下代码应该运行良好(最初由 Peter provost 在 PowerShell 代码存储库上发布):

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;
using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;

namespace HardLinkEnumerator
{
   public static class Kernel32Api
   {
       [StructLayout(LayoutKind.Sequential)]
       public struct BY_HANDLE_FILE_INFORMATION
       {
           public uint FileAttributes;
           public FILETIME CreationTime;
           public FILETIME LastAccessTime;
           public FILETIME LastWriteTime;
           public uint VolumeSerialNumber;
           public uint FileSizeHigh;
           public uint FileSizeLow;
           public uint NumberOfLinks;
           public uint FileIndexHigh;
           public uint FileIndexLow;
       }

       [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
       static extern SafeFileHandle CreateFile(
           string lpFileName,
           [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
           [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
           IntPtr lpSecurityAttributes,
           [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
           [MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
           IntPtr hTemplateFile);

       [DllImport("kernel32.dll", SetLastError = true)]
       static extern bool GetFileInformationByHandle(SafeFileHandle handle, out BY_HANDLE_FILE_INFORMATION lpFileInformation);

       [DllImport("kernel32.dll", SetLastError = true)]
       [return: MarshalAs(UnmanagedType.Bool)]
       static extern bool CloseHandle(SafeHandle hObject);

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

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

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

       [DllImport("kernel32.dll")]
       static extern bool GetVolumePathName(string lpszFileName,
           [Out] StringBuilder lpszVolumePathName, uint cchBufferLength);

       [DllImport("shlwapi.dll", CharSet = CharSet.Auto)]
       static extern bool PathAppend([In, Out] StringBuilder pszPath, string pszMore);

       public static int GetFileLinkCount(string filepath)
       {
           int result = 0;
           SafeFileHandle handle = CreateFile(filepath, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, FileAttributes.Archive, IntPtr.Zero);
           BY_HANDLE_FILE_INFORMATION fileInfo = new BY_HANDLE_FILE_INFORMATION();
           if (GetFileInformationByHandle(handle, out fileInfo))
               result = (int)fileInfo.NumberOfLinks;
           CloseHandle(handle);
           return result;
       }

       public static string[] GetFileSiblingHardLinks(string filepath)
       {
           List<string> result = new List<string>();
           uint stringLength = 256;
           StringBuilder sb = new StringBuilder(256);
           GetVolumePathName(filepath, sb, stringLength);
           string volume = sb.ToString();
           sb.Length = 0; stringLength = 256;
           IntPtr findHandle = FindFirstFileNameW(filepath, 0, ref stringLength, sb);
           if (findHandle.ToInt32() != -1)
           {
               do
               {
                   StringBuilder pathSb = new StringBuilder(volume, 256);
                   PathAppend(pathSb, sb.ToString());
                   result.Add(pathSb.ToString());
                   sb.Length = 0; stringLength = 256;
               } while (FindNextFileNameW(findHandle, ref stringLength, sb));
               FindClose(findHandle);
               return result.ToArray();
           }
           return null;
       }

   }
}

关于c# - 列出文件的硬链接(hard link)(在 C# 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4193309/

相关文章:

c# - 后台 worker

linux - 如何从内置 bin 重定向以执行 AppImage?

unix - Unix中的硬链接(hard link)和符号链接(symbolic link)

c# - 关于显示启动画面图像的 WPF 问题

c# - 为什么我的服务器无法响应来自Web浏览器的客户端请求?

c# - WPF编译错误

c++ - Windows XP中如何判断一个驱动器是否支持硬链接(hard link)?

c# - .NET 连接目录和 DirectInfo 问题

arrays - 如何将结点中的值作为数组返回?