问题
In Windows 7 theres a possibility for getting file's previous versions like in the below image:
Is there any way to retrieve file's previous version by code? because I couldn't find any API.

Thanks advanced! =]
回答1:
There are several tags listed with this question. So it is unclear if a strictly c/c++ approach is desired, or if scripting etc will work. In any case...
Here are some links that will hopefully point in the right direction:
- On the MSDN site, there is documentation and example code referring to shadow copy API.
- Here is a Link to the concept of shadow copy service.
- Here is a description of how you can command line, or program script to recover files from shadow copy.
- Using the API link above with the structures found here will provide you with a way to get information about a particular file, volume etc.
- Finally, Here is a link talking about the Volrest utility from Windows Server 2003 Resource Kit tools, including information on how you can "see a list of available previous versions of [a] folder".
回答2:
So after some searching, and thanks to @ryyker and @Ben directions I was able to find out the answer:
For example for file: C:\SomeFolder\SomeFile.exe
From cmd (run as administrator):
vssadmin list shadows for=C:\
For programmatic solution you can run it with:
CreateProcessW(NULL,L"cmd.exe /c \"vssadmin list shadows for=C:\\ > C:\\someTmpFile.txt\"",...);
Read and parse the created file.

Here above you get a list of shadow copies (kind of "Previous versions" containers).
Refer to the appropriate "Shadow Copy Volume" row (the version you want) and append the remaining file path after the volume name:
\\ Previous version path = \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy3\SomeFolder\SomeFile.exe"
wchar_t* prevPath = L"\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy3\\SomeFolder\\SomeFile.exe";
Now you can read the file with the well known WIN32API functions CreateFile and ReadFile. (Create and Read file example from MSDN: EXAMPLE)
Make sure to use the UNICODE versions of that functions as the ASCII version may lack support of "\?\" paths.
Good luck! =]
来源:https://stackoverflow.com/questions/23528215/get-files-previous-versions-from-winapi