C # delete files occupied by programs, clear and delete files, permanently delete files, and remove files
Open a file in shared read/write mode
FileStream inputStream = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); p.BackgroundImage = new Bitmap(inputStream); inputStream.Dispose();
Delete an object
Public void WipeFile (string filename, int timesToWrite) {try {if (File. exists (filename) {// sets the File attribute to normal to prevent the File from being read-only. setAttributes (filename, FileAttributes. normal); // calculate the number of sectors double sectors = Math. ceiling (new FileInfo (filename ). length/512.0); // create a virtual cache byte of the same size [] dummyBuffer = new byte [512]; // create an encrypted random number generator RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider (); // FileStream inputStream = new FileStream (filename, FileMode. open, FileAccess. write, FileShare. readWrite); for (int currentPass = 0; currentPass <timesToWrite; currentPass ++) {// file stream location inputStream. position = 0; // loop all sectors for (int sectorsWritten = 0; sectorsWritten <sectors; sectorsWritten ++) {// fill the junk data in the stream rng. getBytes (dummyBuffer); // write data to inputStream in the file stream. write (dummyBuffer, 0, dummyBuffer. length) ;}} // clear the file inputStream. setLength (0); // close the file stream inputStream. close (); // to clear the original date, DateTime dt = new DateTime (2037, 1, 1, 0, 0, 0); File. setCreationTime (filename, dt); File. setLastAccessTime (filename, dt); File. setLastWriteTime (filename, dt); // delete the File. delete (filename) ;}} catch (Exception ){}}