junction - List Hard Links of a file (in C#) -
i want write program shows files of other drive hard links. want keep both hardlinks consistent in filename , other things have function/method can list current hard links of file.
so example:
i have file c:\file.txt , second hard link d:\file.txt.
rename d:\file.txt d:\file_new.txt. want able rename hardlink on c drive well.
need function returns d:\file_new.txt there following hardlinks:
c:\file.txt
d:\file_new.txt
can rename hard link on c:\ d:\file_new.txt
so need hard links of physical file. or: hard links of file addressed hard link.
hope can help!
edit:
oliver noticed hard links can't used on differnt disks. thanks... extend question to: need? junction points? symbolic links? should work files not folders!
the following code should work (originally postet peter provost on powershell code repository):
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) { { 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; } } }
Comments
Post a Comment