C # Removes the files used by the program, cleans the deleted files, deletes the files completely, and unlocks the files
Open in shared read-write mode when the file is opened
FileStream InputStream = new FileStream (name, FileMode.Open, FileAccess.Read, fileshare.readwrite); P.backgroundimage = new Bitmap (InputStream); Inputstream.dispose (); Delete file public void Wipefile (string filename, int timestowrite) {try { The IF (file.exists (filename)) {//Settings file property is normal, which is to prevent the file from being read-only File.set Attributes (filename, fileattributes.normal); Calculates the number of sectors double sectors = math.ceiling (new FileInfo (filename). length/512.0); Create a virtual cache of the same size byte[] Dummybuffer = new byte[512]; Create an encrypted random number generator RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider (); Open this file for FileStream FileStream InputStream = new FileStream (filename, FileMode.Open, Fileaccess.wri TE, Fileshare.readwrite); for (int curRentpass = 0; Currentpass < Timestowrite; currentpass++) {//file stream location inputstream.position = 0; Loop all sectors for (int sectorswritten = 0; Sectorswritten < sectors; sec torswritten++) {//fill the garbage data into the RNG in the stream. GetBytes (Dummybuffer); Writes to the file stream Inputstream.write (dummybuffer, 0, dummybuffer.length); }}//Empty file Inputstream.setlength (0); Close file stream Inputstream.close (); Clearing the original date requires a datetime dt = new DateTime (2037, 1, 1, 0, 0, 0); File.setcreationtime (filename, DT); File.setlastaccesstime (filename, DT); File.setlaStwritetime (filename, DT); Delete file file.delete (filename); }} catch (Exception) {}}
Reprinted from Http://blog.csdn.net/pigkeli/article/details/28877279?utm_source=tuicool
C # Delete files, clean delete files