C # Delete files, clean delete files

Source: Internet
Author: User

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

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.