Info File vs. Hardlink
-
Hi. All i want to do is something like
...
foreach (string f in Directory.GetFiles(@"X:\someDir"))
{
Console.Write(f);
if (IsHardlink(f) == false) Console.WriteLine(" is real fileentry");
else Console.WriteLine(" is hardlink and the target is " + GetTargetName(f));
}
...
private bool IsHardLink(string filePath)
{
???
}
private string GetTargetName(string filePath)
{
???
}I 'googled' for days, found tons of articles 'how to create hardlinks, junctions' etc, also many examples for 'get the linkcount of a given file" (the target), but nothing for my need. Please help, give me advice, point me in a direction. Thank you
-
Hi. All i want to do is something like
...
foreach (string f in Directory.GetFiles(@"X:\someDir"))
{
Console.Write(f);
if (IsHardlink(f) == false) Console.WriteLine(" is real fileentry");
else Console.WriteLine(" is hardlink and the target is " + GetTargetName(f));
}
...
private bool IsHardLink(string filePath)
{
???
}
private string GetTargetName(string filePath)
{
???
}I 'googled' for days, found tons of articles 'how to create hardlinks, junctions' etc, also many examples for 'get the linkcount of a given file" (the target), but nothing for my need. Please help, give me advice, point me in a direction. Thank you
Detecting a junction should be as simple as:
private static bool IsHardLink(string filePath)
{
var attributes = File.GetAttributes(filePath);
return attributes.HasFlag(FileAttributes.ReparsePoint);
}Retrieving the target is more complicated, but Jeff Brown has a solution: Manipulating NTFS Junction Points in .NET[^]
private static string GetTargetName(string filePath)
{
return JunctionPoint.GetTarget(filePath);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Detecting a junction should be as simple as:
private static bool IsHardLink(string filePath)
{
var attributes = File.GetAttributes(filePath);
return attributes.HasFlag(FileAttributes.ReparsePoint);
}Retrieving the target is more complicated, but Jeff Brown has a solution: Manipulating NTFS Junction Points in .NET[^]
private static string GetTargetName(string filePath)
{
return JunctionPoint.GetTarget(filePath);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
JunctionPoint deals with Directory. My need is file symlink, espec HardLink, which means that the link resides on the same volume as the target but in another Directory.
With Windows all kinds of links are indicated by the
FILE_ATTRIBUTE_REPARSE_POINT
flag:File Attribute Constants (Windows)[^]:
A file or directory that has an associated reparse point, or a file that is a symbolic link.
To differentiate between file and directory links use the corresponding attribute flag. To check if a file is on the same drive (hard link), you have to resolve the resparse point to get the target (which you want anyway). I have no solution for C# but the source of the article mentioned by Richard may contain useful information. A solution for C/C++ can be found at HowTo: Correctly read reparse data in Vista | Jochen Kalmbach's Blog[^].
-
Hi. All i want to do is something like
...
foreach (string f in Directory.GetFiles(@"X:\someDir"))
{
Console.Write(f);
if (IsHardlink(f) == false) Console.WriteLine(" is real fileentry");
else Console.WriteLine(" is hardlink and the target is " + GetTargetName(f));
}
...
private bool IsHardLink(string filePath)
{
???
}
private string GetTargetName(string filePath)
{
???
}I 'googled' for days, found tons of articles 'how to create hardlinks, junctions' etc, also many examples for 'get the linkcount of a given file" (the target), but nothing for my need. Please help, give me advice, point me in a direction. Thank you
For hard links, you can't do what you want. This Stack Overflow answer[^] explains why:
Quote:
On NTFS all files are hard links. You can detect that a file has multiple hard links pointing to it, but there's no "real file" that it points to. You can think of hard links as just different names for the same file.
So for example if you have
file1.txt
and you create a hard linkfile2.txt
that points to the first file, both are hard links and the 'target' is the data on your disk. What you can do, is detecting if a file is a symbolic link: c# - Check if a file is real or a symbolic link - Stack Overflow[^]The quick brown ProgramFOX jumps right over the
Lazy<Dog>
.