Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Info File vs. Hardlink

Info File vs. Hardlink

Scheduled Pinned Locked Moved C#
visual-studiohelptutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    my Nick
    wrote on last edited by
    #1

    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

    Richard DeemingR T 2 Replies Last reply
    0
    • M my Nick

      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

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        M Offline
        M Offline
        my Nick
        wrote on last edited by
        #3

        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.

        J 1 Reply Last reply
        0
        • M my Nick

          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.

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          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[^].

          1 Reply Last reply
          0
          • M my Nick

            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

            T Offline
            T Offline
            Thomas Daniels
            wrote on last edited by
            #5

            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 link file2.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>.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups