C # delete files occupied by programs, force delete files, thoroughly delete files, and cancel file occupation

Source: Internet
Author: User

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 ){}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.