Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7781997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:24:08+00:00 2026-06-01T19:24:08+00:00

Possible Duplicate: Unique file identifier in windows I need to retrieve a unique identifier

  • 0

Possible Duplicate:
Unique file identifier in windows

I need to retrieve a unique identifier for certain files on the computer, and have only came across the Win32 GetFileInformationByHandle function. How can I accomplish this with the. NET framework?

Update: I need a persistent id that will not change if the file is moved, updated, renamed, etc.

Update2: How can the same be accomplished with folders?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:24:09+00:00Added an answer on June 1, 2026 at 7:24 pm

    Here’s some code from Ashley Henderson I copied from this answer. It implies two approaches which both return the same unique identifier.

    public class WinAPI
        {
            [DllImport("ntdll.dll", SetLastError = true)]
            public static extern IntPtr NtQueryInformationFile(IntPtr fileHandle, ref IO_STATUS_BLOCK IoStatusBlock, IntPtr pInfoBlock, uint length, FILE_INFORMATION_CLASS fileInformation);
    
            public struct IO_STATUS_BLOCK
            {
                uint status;
                ulong information;
            }
            public struct _FILE_INTERNAL_INFORMATION {
              public ulong  IndexNumber;
            } 
    
            // Abbreviated, there are more values than shown
            public enum FILE_INFORMATION_CLASS
            {
                FileDirectoryInformation = 1,     // 1
                FileFullDirectoryInformation,     // 2
                FileBothDirectoryInformation,     // 3
                FileBasicInformation,         // 4
                FileStandardInformation,      // 5
                FileInternalInformation      // 6
            }
    
            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern bool GetFileInformationByHandle(IntPtr hFile,out BY_HANDLE_FILE_INFORMATION lpFileInformation);
    
            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;
            }
      }
    
      public class Test
      {
           public ulong ApproachA()
           {
                    WinAPI.IO_STATUS_BLOCK iostatus=new WinAPI.IO_STATUS_BLOCK();
    
                    WinAPI._FILE_INTERNAL_INFORMATION objectIDInfo = new WinAPI._FILE_INTERNAL_INFORMATION();
    
                    int structSize = Marshal.SizeOf(objectIDInfo);
    
                    FileInfo fi=new FileInfo(@"C:\Temp\testfile.txt");
                    FileStream fs=fi.Open(FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
    
                    IntPtr res=WinAPI.NtQueryInformationFile(fs.Handle, ref iostatus, memPtr, (uint)structSize, WinAPI.FILE_INFORMATION_CLASS.FileInternalInformation);
    
                    objectIDInfo = (WinAPI._FILE_INTERNAL_INFORMATION)Marshal.PtrToStructure(memPtr, typeof(WinAPI._FILE_INTERNAL_INFORMATION));
    
                    fs.Close();
    
                    Marshal.FreeHGlobal(memPtr);   
    
                    return objectIDInfo.IndexNumber;
    
           }
    
           public ulong ApproachB()
           {
                   WinAPI.BY_HANDLE_FILE_INFORMATION objectFileInfo=new WinAPI.BY_HANDLE_FILE_INFORMATION();
    
                    FileInfo fi=new FileInfo(@"C:\Temp\testfile.txt");
                    FileStream fs=fi.Open(FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
    
                    WinAPI.GetFileInformationByHandle(fs.Handle, out objectFileInfo);
    
                    fs.Close();
    
                    ulong fileIndex = ((ulong)objectFileInfo.FileIndexHigh << 32) + (ulong)objectFileInfo.FileIndexLow;
    
                    return fileIndex;   
           }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to get a unique computer identifier in Java (like disk id
Possible Duplicate: optimal algorithm for finding unique divisors I have asked this question before,
Possible Duplicate: Determining if an unordered vector<T> has all unique elements I have to
Possible Duplicate: Populate unique values in to VBA array from excel I need to
Possible Duplicate: How to generate 8 bytes unique id from GUID? I need a
Possible Duplicate: Unique key generation How to automaticly make a new name of file
Possible Duplicate: Unique random numbers in O(1)? How do I fill an integer array
Possible Duplicate: Returning unique_ptr from functions 20.7.1.2 [unique.ptr.single] defines copy constructor like this :
Possible Duplicate: Which collection for storing unique strings? I am currently using a Dictionary<string,
Possible Duplicate: Was: Not unique table :: Now: #1054 - Unknown column - can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.