File system GUID for a file

  • Thread starter Thread starter Tarren
  • Start date Start date
T

Tarren

Hi:

I need to get the unique GUID for a file, so I am using API calls through
System.InteropServices

I am using
BY_HANDLE_FILE_INFORMATION

and

dwVolumeSerialNumber

to get the unique FileSystem GUID for a particular file. Is this the
appropriate property? I am getting unique numbers for each file I run it
for, so it looks like the right one, but I am not sure.

The documentation says it is the unique serial for the volume of the file,
but I want the unique serial for the file itself.

Thanks!
 
There is no such thing like a GUID for a file or a file system.
The API your are talking about (I guess, because you did not mention it), is
GetFileInformationByHandle which returns a pointer to a
BY_HANDLE_FILE_INFORMATION structure.
I guess the "unique number" you get, is the pointer value to the structure
in memory, which has nothing to do with a GUID. What exactly are you trying
top achieve?


Willy.


| Hi:
|
| I need to get the unique GUID for a file, so I am using API calls through
| System.InteropServices
|
| I am using
| BY_HANDLE_FILE_INFORMATION
|
| and
|
| dwVolumeSerialNumber
|
| to get the unique FileSystem GUID for a particular file. Is this the
| appropriate property? I am getting unique numbers for each file I run it
| for, so it looks like the right one, but I am not sure.
|
| The documentation says it is the unique serial for the volume of the file,
| but I want the unique serial for the file itself.
|
| Thanks!
|
|
 
Thanks for the response, Willy.

What I am trying to achieve is to index a series of files in a directory
into a database. I need a unique identifier for each field. I cannot use
filename and path, because I do not want to lose my reference to the file,
even if the file name changes.

When I use GetFileInformationByHandle function and reference
dwVolumeSerialNumber it gives me the number, and is the same number per file
even if rerun the program. This would not be the appropriate identifier?

Any thoughts?

Thanks for your help on this. Much appreciated.
 
The HashCode changes if the file is renamed.

When I go through the API using GetFileInformationByHandle the returned
dwVolumeSerialNumber does not change if the file is renamed.

Based on that I wanting a unique identifier for each file on the filesystem
even if a) the filename changes b)the path changes or c) the contents change
would dwVolumeSerialNumber be sufficicient or is not appropriate to use that
as my identifier?

Thanks.
 
The dwVolumeSerialNumber is a volume serial number, all files on that volume
will have the same dwVolumeSerialNumber.
Why don't you create an guid using the Guid.NewGuid method and use this to
identify the file?

Willy.

| The HashCode changes if the file is renamed.
|
| When I go through the API using GetFileInformationByHandle the returned
| dwVolumeSerialNumber does not change if the file is renamed.
|
| Based on that I wanting a unique identifier for each file on the
filesystem
| even if a) the filename changes b)the path changes or c) the contents
change
| would dwVolumeSerialNumber be sufficicient or is not appropriate to use
that
| as my identifier?
|
| Thanks.
|
|
| | > thank you - I will check it out
| >
| >
| > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
| > wrote in message | >> Hi,
| >>
| >> Check this thread:
| >>
| >>
http://groups.google.com/group/micr...osoft.public.dotnet.*&rnum=3#5ef72cef1b0849a2
| >>
| >>
| >> Cheers,
| >>
| >> --
| >> Ignacio Machin,
| >> ignacio.machin AT dot.state.fl.us
| >> Florida Department Of Transportation
| >>
| >>
| >> | >>> Thanks for the response, Willy.
| >>>
| >>> What I am trying to achieve is to index a series of files in a
directory
| >>> into a database. I need a unique identifier for each field. I cannot
| >>> use filename and path, because I do not want to lose my reference to
the
| >>> file, even if the file name changes.
| >>>
| >>> When I use GetFileInformationByHandle function and reference
| >>> dwVolumeSerialNumber it gives me the number, and is the same number
per
| >>> file even if rerun the program. This would not be the appropriate
| >>> identifier?
| >>>
| >>> Any thoughts?
| >>>
| >>> Thanks for your help on this. Much appreciated.
| >>>
| >>>
| >>> | >>>> There is no such thing like a GUID for a file or a file system.
| >>>> The API your are talking about (I guess, because you did not mention
| >>>> it), is
| >>>> GetFileInformationByHandle which returns a pointer to a
| >>>> BY_HANDLE_FILE_INFORMATION structure.
| >>>> I guess the "unique number" you get, is the pointer value to the
| >>>> structure
| >>>> in memory, which has nothing to do with a GUID. What exactly are you
| >>>> trying
| >>>> top achieve?
| >>>>
| >>>>
| >>>> Willy.
| >>>>
| >>>>
| >>>> | >>>> | Hi:
| >>>> |
| >>>> | I need to get the unique GUID for a file, so I am using API calls
| >>>> through
| >>>> | System.InteropServices
| >>>> |
| >>>> | I am using
| >>>> | BY_HANDLE_FILE_INFORMATION
| >>>> |
| >>>> | and
| >>>> |
| >>>> | dwVolumeSerialNumber
| >>>> |
| >>>> | to get the unique FileSystem GUID for a particular file. Is this
the
| >>>> | appropriate property? I am getting unique numbers for each file I
| >>>> run it
| >>>> | for, so it looks like the right one, but I am not sure.
| >>>> |
| >>>> | The documentation says it is the unique serial for the volume of
the
| >>>> file,
| >>>> | but I want the unique serial for the file itself.
| >>>> |
| >>>> | Thanks!
| >>>> |
| >>>> |
| >>>>
| >>>>
| >>>
| >>>
| >>
| >>
| >
| >
|
|
 
Hi:

I thought that dwVolumeSerialNumber would give me a serial number for the
volume as well.

However, it is giving me a different value for each file, which remains even
if I change the filename. If I make a NEW file with the same name, that NEW
file gets a different value.

What I think it might be is that it is giving me a serial number for the
file specific to that volume, not a serial number for the volume itself?

Any thoughts?

If I create a GUID for the file, how does it key to the file? If I move the
file around on the file system, update the contents of the file, etc, would
it stick?

Thanks
 
Tarren said:
I need to get the unique GUID for a file, so I am using API calls through
System.InteropServices
I am using
BY_HANDLE_FILE_INFORMATION
dwVolumeSerialNumber
to get the unique FileSystem GUID for a particular file.

As others have said, Windows doesn't offer such information. (unix
does; it's called "inode").

Sometimes people ask for unique identifiers so they can tell whether
two filepaths point to the same file, e.g.
c:\docs\file~1.txt
\\mycomp\share\docs\file with contents.txt
may both point to the same file.

For these purposes, the Windows solution is to open a handle for the
first file, and for the second file, and use the
BY_HANDLE_FILE_INFORMATION on both. If both handles have the same
dwVolumeSerialNumber : nFileIndexHigh.nFileIndexLow
then it means that both paths pointed to the same file.

However, the nFileIndex fields are NOT persistent: if you close both
handles, and then open them again, then you'll often get a different
nFileIndex.

(NB. if you think that dwVolumeSerialNumber is giving you different
numbers for two files on the same hard disk partition, then I think
you're mistaken! check your code carefully!)


Finally, if you have a program design which depends on knowing this
information, then I think it's a bad program design... maybe you
should post here to explain the reasons why you think you need a
unique GUID, and people can post different system-designs which don't
need it.
 
There must be something wrong with your code, the dwVolumeSerialNumber is
the "volume serial" number and as such doesn't change (unless you format the
volume). All files on a volume will return the same value (the value isn't
even a file attribute, it's a volume attribute).
Anyway, you can't use this API to uniquely identify a file on a FS.

Willy.

| Hi:
|
| I thought that dwVolumeSerialNumber would give me a serial number for the
| volume as well.
|
| However, it is giving me a different value for each file, which remains
even
| if I change the filename. If I make a NEW file with the same name, that
NEW
| file gets a different value.
|
| What I think it might be is that it is giving me a serial number for the
| file specific to that volume, not a serial number for the volume itself?
|
| Any thoughts?
|
| If I create a GUID for the file, how does it key to the file? If I move
the
| file around on the file system, update the contents of the file, etc,
would
| it stick?
|
| Thanks
|
|
| | > The dwVolumeSerialNumber is a volume serial number, all files on that
| > volume
| > will have the same dwVolumeSerialNumber.
| > Why don't you create an guid using the Guid.NewGuid method and use this
to
| > identify the file?
| >
| > Willy.
| >
| > | > | The HashCode changes if the file is renamed.
| > |
| > | When I go through the API using GetFileInformationByHandle the
returned
| > | dwVolumeSerialNumber does not change if the file is renamed.
| > |
| > | Based on that I wanting a unique identifier for each file on the
| > filesystem
| > | even if a) the filename changes b)the path changes or c) the contents
| > change
| > | would dwVolumeSerialNumber be sufficicient or is not appropriate to
use
| > that
| > | as my identifier?
| > |
| > | Thanks.
| > |
| > |
| > | | > | > thank you - I will check it out
| > | >
| > | >
| > | > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT
dot.state.fl.us>
| > | > wrote in message | > | >> Hi,
| > | >>
| > | >> Check this thread:
| > | >>
| > | >>
| >
http://groups.google.com/group/micr...osoft.public.dotnet.*&rnum=3#5ef72cef1b0849a2
| > | >>
| > | >>
| > | >> Cheers,
| > | >>
| > | >> --
| > | >> Ignacio Machin,
| > | >> ignacio.machin AT dot.state.fl.us
| > | >> Florida Department Of Transportation
| > | >>
| > | >>
| > | >> | > | >>> Thanks for the response, Willy.
| > | >>>
| > | >>> What I am trying to achieve is to index a series of files in a
| > directory
| > | >>> into a database. I need a unique identifier for each field. I
| > cannot
| > | >>> use filename and path, because I do not want to lose my reference
to
| > the
| > | >>> file, even if the file name changes.
| > | >>>
| > | >>> When I use GetFileInformationByHandle function and reference
| > | >>> dwVolumeSerialNumber it gives me the number, and is the same
number
| > per
| > | >>> file even if rerun the program. This would not be the appropriate
| > | >>> identifier?
| > | >>>
| > | >>> Any thoughts?
| > | >>>
| > | >>> Thanks for your help on this. Much appreciated.
| > | >>>
| > | >>>
| > message
| > | >>> | > | >>>> There is no such thing like a GUID for a file or a file system.
| > | >>>> The API your are talking about (I guess, because you did not
| > mention
| > | >>>> it), is
| > | >>>> GetFileInformationByHandle which returns a pointer to a
| > | >>>> BY_HANDLE_FILE_INFORMATION structure.
| > | >>>> I guess the "unique number" you get, is the pointer value to the
| > | >>>> structure
| > | >>>> in memory, which has nothing to do with a GUID. What exactly are
| > you
| > | >>>> trying
| > | >>>> top achieve?
| > | >>>>
| > | >>>>
| > | >>>> Willy.
| > | >>>>
| > | >>>>
| > | >>>> | > | >>>> | Hi:
| > | >>>> |
| > | >>>> | I need to get the unique GUID for a file, so I am using API
calls
| > | >>>> through
| > | >>>> | System.InteropServices
| > | >>>> |
| > | >>>> | I am using
| > | >>>> | BY_HANDLE_FILE_INFORMATION
| > | >>>> |
| > | >>>> | and
| > | >>>> |
| > | >>>> | dwVolumeSerialNumber
| > | >>>> |
| > | >>>> | to get the unique FileSystem GUID for a particular file. Is
this
| > the
| > | >>>> | appropriate property? I am getting unique numbers for each
file
| > I
| > | >>>> run it
| > | >>>> | for, so it looks like the right one, but I am not sure.
| > | >>>> |
| > | >>>> | The documentation says it is the unique serial for the volume
of
| > the
| > | >>>> file,
| > | >>>> | but I want the unique serial for the file itself.
| > | >>>> |
| > | >>>> | Thanks!
| > | >>>> |
| > | >>>> |
| > | >>>>
| > | >>>>
| > | >>>
| > | >>>
| > | >>
| > | >>
| > | >
| > | >
| > |
| > |
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top