Monday, July 27, 2009

How do i undelete files from a harddrive in visual basic? I also know c++ and C# so explain however u r comfl

been everywhere like amazon and msdn forums and no answers yet. looking for programatically undelete files in vb.net I'm doing a project for my portfolio if anyone wants to work on it with me go ahead and email me through yahoo answers. --thanks jay

How do i undelete files from a harddrive in visual basic? I also know c++ and C# so explain however u r comfl
A trip to the Recycle Bin—and beyond:





Follow the Windows APIs...





The following contents explores various concepts and API functions that are useful in order to be able to perform operations related to the Recycle Bin.





I don't know whether you want to undelete files that still exist in the Recycle Bin, or whether you want to recover files that have been already emptied out of the Recycle Bin. Regardless, the information here will assist you either way. The first link below applies to the former situation; whereas, the second link applies to the latter.


______________________________________...





This web page is a must-read. It details how the Recycle Bin works. It provides a comprehensive explanation concerning programmatical manipulation of the Recycle Bin. There are also code snippets that are designed to manipulate the contents of the Recycle Bin. The site is located at:





http://www.codeproject.com/shell/recycle...


______________________________________...





Another must read: This next site has the code to programmatically discover the files that have been emptied from the Recycle Bin, and recover them. This is accomplished by issuing an API call—SHGetFileInfo—on the PIDL. (The PIDL is returned by an EnumObjects call.) You end up gaining access to the image index from the Shell's ImageList. Next, you request the Shell to share its ImageList with your application. The rest is not so painful! Check it out:





http://www.codeproject.com/shell/recycle...


______________________________________...





Some relevant Windows API functions:





1) SHEmptyRecycleBin


2) SHQueryReycleBin


3) SHQueryReycleBin


4) SHGetSpecialFolderLocation


______________________________________...





When Explorer sends a file or folder to the Recycle Bin, the item's data clusters don't move. Instead, Explorer simply moves the item's directory entry into the appropriate physical bin folder on the same drive, and then renames the directory entry. Windows stores the original names and locations in a hidden index file named INFO2, which is stored in each physical bin folder. The names of items within folders sent to the Recycle Bin are not changed and are not stored in the index file.





Because the structure of the INFO2 index file is not documented and is subject to change without notice, you does not attempt to manipulate it in any way. you cannot rename or delete any file or folder referenced by the INFO2 file without putting the index out of synch. But you can rename and delete items inside folders within the Recycle Bin, and can change the size or contents of files at the root level of the bin without affecting the index.





Use the SHGetSpecialFolderLocation Shell API function to get the location of the Recycle Bin. SHQueryRecycleBin retrieves information about how many files (or other items) are currently in the Recycle Bin as well as how much disk space they consume. This function will work with the Recycle Bin specific to a certain drive, as well as work with the Recycle Bin as considered over the entire system.


Let's say that you have created Restore_File() and Restore_Folder() routines to assist you in this project.


The SHQueryRecycleBin API is used to determine the contents of the bins.





For each drive, use the SHQueryReycleBin API function to determine whether the Recycle Bin on that drive is empty. If not, check whether the drive is formatted using NTFS, and then constructs th path to the bin's folder on that drive. Once you have has the proper bin folder name, use the FindFirstFile and FindNextFile API functions in a While loop to walk through that folder, calling either Restore_File or Restore_Folder as appropriate for each file or folder it encounters. The While loop skips over both the INFO2 file and the Desktop.ini file, if they exist, because these files belong to Windows. If the file or folder is at the root level of the bin folder, it is not renamed or deleted after shredding. your program should call itself recursively—to process any subfolders it encounters.





______________________________________...





SHFileOperation function, which is exported by Shell32.dll. SHFileOperation is supported under Windows 95/98/Me/NT4.0/2000 and allows copying, moving, renaming, and deleting objects in the file system. You can also use it to delete files by sending them to the Recycle Bin so that users can restore them if necessary.





By calling SHFileOperation, you can leverage the existing dialogs for moving files and providing user feedback about the status of the process. To use the SHFileOperation function in a Visual Basic program, you must include the following Declare statement:





Public Declare Function SHFileOperation Lib _


"shell32.dll" Alias "SHFileOperationA" (lpFileOp _


As SHFILEOPSTRUCT) As Long





Sending files to the Recycle Bin requires setting two options: FO_DELETE and FO_ALLOWUNDO.


______________________________________...





C++ code:





#include %26lt;shellapi.h%26gt;





SHFILEOPSTRUCT fop;





// initialize all data required for the copy


fop.hwnd = NULL;


fop.wFunc = FO_COPY;


fop.pFrom = "C:\\autoexec.bat\0C:\\config.sys\0";


fop.pTo = "A:\\";


fop.fFlags = FOF_ALLOWUNDO;


// remaining entries are output variables, no need to initialize





SHFileOperation(%26amp;fop);


______________________________________...





SHFileOperation function:





You can use it to work to perform operations related to the Recycle Bin.





The key to this technique is the SHFileOperation function, which is exported by Shell32.dll. SHFileOperation is supported under Windows 95/98/Me/NT4.0/2000 and allows copying, moving, renaming, and deleting objects in the file system. You can also use it to delete files by sending them to the Recycle Bin so that users can restore them if necessary.





By calling SHFileOperation, you can leverage the existing dialogs for moving files and providing user feedback about the status of the process. To use the SHFileOperation function in a Visual Basic program, you must include the following Declare statement:





Public Declare Function SHFileOperation Lib _


"shell32.dll" Alias "SHFileOperationA" (lpFileOp _


As SHFILEOPSTRUCT) As Long








A single API SHFileOperation will copy, move, rename and delete, both files and folders.





SHFileOperation takes only one argument, which contains all the relevant information in a SHFILEOPSTRUCT. The only thing in this code snippet that can be considered as remotely "tricky" is the construction of the pFrom string with the files to copy from. First note that full paths must be specified for all files. The pitfall with full paths strings in C++ that catches everybody off guard is the humble backslash. You need two of them ("\\") in string constants, else they are misinterpreted as escape sequences.





The second issue is multiple source files and how to separate them. The architects of SHFileOperation came up with a simple design: a large buffer that contains consecutive NULL-terminating strings to the individual files. The last file is identified by an extra zero byte; this way we don't need a separate argument for the number of files in the pFrom buffer. In the code above the \0 escape sequence is used to add extra zero bytes in the string. Note that even if you just want to copy a single file you'd still need to use an extra zero to terminate the string, e.g. "c:\\file.txt\0".





Other than that, managing files with SHFileOperation is pretty straightforward. You select what to do by setting the wFunc member. You can fine tune operations using various options for the fFlags member. All FOF_xxx constants have self-documenting names. I don't have much to say on the subject that the online docs won't cover. Some issues that perhaps need clarification follow:





Wildcards. Another way to copy multiple files is to specify a wildcard like "c:\\*.bat" in pFrom member. FOF_FILESONLY flag may prove handy here. If you want to test whether M/S developers are worth their salaries, try passing "c:\\*.bat\0c:\\*.*\0" and see if some files will be doubly-copied or what.





Folders: To copy/delete/etc. a whole folder, just include its name in pFrom member. The operation will affect all folder contents, including its subfolders, if any. If you need extra control, try the FOF_NORECURSION flag.





Confirmations: By default, shell will ask for user confirmation whenever a file is about to be nuked, be it by deletion or by being overwritten. If you want to play it risky, use the FOF_NOCONFIRMATION flag, which suppresses the dialogs. I personally leave confirmations on, especially for FO_DELETE, because you never know when your fingers will trip pressing %26lt;Del%26gt; instead of the intended %26lt;Esc%26gt; (given enough time, all eventualities, however improbable, are going to be realized, so prepare early %26lt;g%26gt;). A safe way to minimize delete confirmations is to edit Recycle Bin's properties and switch off confirmations for when items are merely moved into the bin.





Undo provision. Using FOF_ALLOWUNDO flag, the shell saves extra information that can undo certain file operations. Obviously if you overwrite some file you won't get it back, but it can bail you out of certain mishaps. When used for delete operations, FOF_ALLOWUNDO sends items to the Recycle Bin. If omitted, items are permanently deleted.





ADVANCED: Exactly how and where this undo information is kept is a mystery. There is no API, no COM interfaces, no published information whatsoever. Mikrosoft didn't have the vision to make this information available to programs except their own explorer. Still you can do your bit and users can open an explorer window and undo an operation that originated from a different program, i.e. yours.


Useless info. All the members in SHFILEOPSTRUCT that are filled after the operation is finished are next to useless. fAnyOperationsAborted won't tell you specifically which of them were aborted. hNameMappings is an erratic little number which will seldom be returned, even if you explicitly ask for it via FOF_WANTMAPPINGHANDLE. I've heard that you would get a valid handle if the operation resulted in creating items like "Copy (2) of file.txt", but still this eventuality is better handled within the change notification framework, to be presented shortly.


______________________________________...





'This web page actually has the link to the VB code that can perform undelete operations.





http://www.experts-exchange.com/Programm...





Cost you a one month membership $12.00





There are five links at the very top of this web page. The three of special interest are: "Undelete," "Recycle bin file info," and "How to Undelete Files." You should seriously consider checking out the last one listed here.


______________________________________...





Some other related sites:





Info about the how the Recycle Bin works:


http://www.osronline.com/showThread.cfm?...





Moving files to the Recycle Bin, the VB code: http://www.a1vbcode.com/vbtip-19.asp





SHQueryReycleBin: http://www.math.msu.su/~vfnik/WinApi/s/s...





Delete and Undelete Recycle Bin items: http://www.codeproject.com/shell/recycle...
Reply:I don't have my laptop with VS.Net 2005 with me, but in C#, you might be able to access the Recycle Bin with:


"Environment.GetFolderPath(


Environment. SpecialFolder. Personal) " - this should return a lot of folder paths.





and maybe you can see what options you have there.





hope this directs you in the right path..


v3ks


Sorry about the spaces in the line of code. For some reason, when I don't have those spaces, it doesn't display properly.
Reply:THE FOLLOWING ONLY WORKS ON WINDOWS!!! NOT MAC!!!


______________________________________...





Go to your hard drive,


go to your folder called ''Windows''


under that find a folder named ''temp'' or ''temporarily"





And you can find your missing file!


______________________________________...


Easier Way:





go to microsoft.com


download, or update the latest


recommended:


2005


2008 (includes all versions)





there is NO 2006 version!
Reply:well, you are probably aware that Windows doesn't actually delete files initially. when a user deletes a file it is put into a special folder called the Recycle Bin. c:\RECYCLER\{machineguid-userguid-index}... however, i'm fairly sure that files deleted through an application are just deleted. more than likely the various programming APIs don't utilize the recycle bin. for C#, i'm fairly certain this is true.





there are a number of companies that have developed undelete solutions that can be purchased and used by the various programming languages. i don't know how useful they would be for you. if i were coding a specific function for an application i might actually write a custom solution for this.





C#.NET


i might start by overriding the System.IO.File.Delete function with my own function that would instead just move the file into a application specific folder, then if the user wants to "undelete" the file, i could retrieve it from that area and place it back into its previous location.





it's a start...





[edit] although i doubt it will, i'd be interested to to hear if v3ks' solutions works.


No comments:

Post a Comment