1

im working on a project where in i need to populate some files from the server and show them in a webrowser to the user. Where user would be allowed to do basic CRUD operation on the file The problem is that i cannot assign the name of the file/folder as unique id which i need in case a user deletes/renames a file i should be able to identify the file later.

My question is that is there anything unique about any file/folder in NTFS that is unique about them and that can be used? and how that information can be accessed using .NET c#.

Came across the BY_HANDLE_FILE_INFORMATION structure but it probably needs me to pinvoke.

UPDATE 1 - tried looking at the feasibility to use nFileIndexHigh and nFileIndexLow but they would need me to do a pinvoke and also this method cannot be used with folders one link when they mention folders Unique Folder Identifier tells me that it isnt possible.

Community
  • 1
  • 1
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
  • Don't get me wrong but the path of the file is always unique, no two files can have the same path – Vamsi Sep 14 '12 at 13:17
  • If you want to associate custom data with a file you can read/write whatever you like to an ntfs alternate data stream http://stackoverflow.com/questions/1978298/ntfs-alternate-data-streams-good-or-bad-idea - there are lots of pinvoke examples – Alex K. Sep 14 '12 at 13:17
  • @VamsiKrishna - but their name changes as soon as user would give a rename command. that means ids of all the children changes in case of a folder and this is problematic – Parv Sharma Sep 14 '12 at 13:19
  • @AlexK. - this interface isnt the onlyway a user can create a file and some of the other ways are not under my control. therefore i cannot grantee that they would always associate some unique data to the file/folder – Parv Sharma Sep 14 '12 at 13:21
  • See http://msdn.microsoft.com/en-us/library/windows/desktop/aa363788(v=vs.85).aspx and look down the bottom for nFileIndexLow adn read the info on it, it describes references of a NTFS file's unique id – Paul Zahra Sep 14 '12 at 13:21
  • @PaulZahra - this is what i have implemented. but when i change name of a folder ids of all the children needs to be changed and that is a problem. with it id by definition is somethig that shudnt change. – Parv Sharma Sep 14 '12 at 13:24
  • Sorry replaced my last comment after reading yours :D ... nFileIndexHigh, nFileIndexLow and the volume serial number uniquely identify a file on a single computer. P.S. These are only available to desktop apps :( – Paul Zahra Sep 14 '12 at 13:27
  • Here (http://stackoverflow.com/questions/410705/best-way-to-determine-if-two-path-reference-to-same-file-in-c-sharp) they suggest to call GetFileInformationByHandle on the files and to compare dwVolumeSerialNumber, nFileIndexHigh and nFileIndexLow – AardVark71 Sep 14 '12 at 13:34
  • @AardVark71 - yeah that can be used but. cant use this method for Folders – Parv Sharma Sep 14 '12 at 13:47
  • That is the solution i posted a link to (above)... the unique id will stay the same wherever the file is placed (on 1 drive), but move drives and it changes (thus you use the low and high indexes along with the volume id), also note that programs like office like to delete and recreate files when u change them (thus screwing over this solution). P.S. Why are you bothered about identifying the folders? (it's not specified in your question – Paul Zahra Sep 14 '12 at 13:53
  • @PaulZahra - other softwares are not a problem. this method cannot be used for folders and the pinvoke are the problem. – Parv Sharma Sep 14 '12 at 13:56
  • See http://stackoverflow.com/questions/3482178/how-do-you-read-the-128-bit-ntfs-file-id-for-a-directory-and-or-file I think your answer lies within that answer – Paul Zahra Sep 14 '12 at 14:02

2 Answers2

0

I think the FILE_INTERNAL_INFORMATION structure, used to query for the file system's 8-byte file reference number for a file, is what you need. The IndexNumber is a number that uniquely identifies a file. You should use P/invoke to query this information.

Centro
  • 3,892
  • 2
  • 25
  • 31
  • File reference numbers, also called file IDs, are guaranteed to be unique only within a static file system. They are not guaranteed to be unique over time, because file systems are free to reuse them. Nor are they guaranteed to remain constant. For example, the FAT file system generates the file reference number for a file from the byte offset of the file's directory entry record (DIRENT) on the disk. Defragmentation can change this byte offset. Thus a FAT file reference number can change over time. – Parv Sharma Sep 14 '12 at 14:11
0

Disregarding the fact that it seems that you don't want to P/Invoke a different approach could be to use the Windows Shell API. This API has the concept of a PIDL which identifies a folder (or "similar" item) in the shell namespace. A PIDL does not change even if the user renames the folder. The drawback is that the Shell API exposes what you see in Windows Explorer, e.g. Desktop is at the root of the hierarchy even though it physically is subfolder somewhere on a disk.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256