mCleaner.Helpers.Wipe.WipeFile C# (CSharp) Méthode

WipeFile() public méthode

Deletes a file in a secure way by overwriting it with random garbage data n times.
public WipeFile ( string filename, int timesToWrite ) : void
filename string Full path of the file to be deleted
timesToWrite int Specifies the number of times the file should be overwritten
Résultat void
        public void WipeFile(string filename, int timesToWrite)
        {
            try
            {
                if (File.Exists(filename))
                {
                    // Set the files attributes to normal in case it's read-only.
                    File.SetAttributes(filename, FileAttributes.Normal);

                    // Calculate the total number of sectors in the file.
                    double sectors = Math.Ceiling(new FileInfo(filename).Length / 512.0);

                    // Create a dummy-buffer the size of a sector.
                    byte[] dummyBuffer = new byte[512];

                    // Create a cryptographic Random Number Generator.
                    // This is what I use to create the garbage data.
                    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

                    // Open a FileStream to the file.
                    FileStream inputStream = new FileStream(filename, FileMode.Open);
                    for (int currentPass = 0; currentPass < timesToWrite; currentPass++)
                    {
                        UpdatePassInfo(currentPass + 1, timesToWrite);

                        // Go to the beginning of the stream
                        inputStream.Position = 0;

                        // Loop all sectors
                        for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++)
                        {
                            UpdateSectorInfo(sectorsWritten + 1, (int)sectors);

                            // Fill the dummy-buffer with random data
                            rng.GetBytes(dummyBuffer);
                            // Write it to the stream
                            inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
                        }
                    }
                    // Truncate the file to 0 bytes.
                    // This will hide the original file-length if you try to recover the file.
                    inputStream.SetLength(0);
                    // Close the stream.
                    inputStream.Close();

                    // As an extra precaution I change the dates of the file so the
                    // original dates are hidden if you try to recover the file.
                    DateTime dt = new DateTime(2037, 1, 1, 0, 0, 0);
                    File.SetCreationTime(filename, dt);
                    File.SetLastAccessTime(filename, dt);
                    File.SetLastWriteTime(filename, dt);

                    File.SetCreationTimeUtc(filename, dt);
                    File.SetLastAccessTimeUtc(filename, dt);
                    File.SetLastWriteTimeUtc(filename, dt);

                    // Finally, delete the file
                    File.Delete(filename);

                    WipeDone();
                }
            }
            catch (Exception e)
            {
                WipeError(e);
            }
        }

Usage Example

        private void StartShredding()
        {
            btnShreddingEnable = true;
            Wipe wiper = new Wipe();
            wiper.WipeErrorEvent += Wiper_WipeErrorEvent;
            wiper.PassInfoEvent += Wiper_PassInfoEvent;
            wiper.SectorInfoEvent += Wiper_SectorInfoEvent;
            wiper.WipeDoneEvent += Wiper_WipeDoneEvent;
            foreach (Model_Shred MdlShared in ShredFilesCollection)
            {

                string full_param = string.Empty;
                if (MdlShared.FilePath != "Recycle Bin")
                {
                    FileAttributes attr = File.GetAttributes(MdlShared.FilePath);
                    if (attr.HasFlag(FileAttributes.Directory))
                    {
                        this.ProgressText = "Started to Shred Folder" + MdlShared.FilePath + " Please Wait.";
                        foreach (string file in Directory.EnumerateFiles(MdlShared.FilePath, "*.*", SearchOption.AllDirectories))
                        {
                            this.ProgressText = "Started to Shred File " + file + " inside folder " + MdlShared.FilePath +
                                                " Please Wait.";
                            wiper.WipeFile(file, 2);
                        }
                        Directory.Delete(MdlShared.FilePath, true);
                    }
                    else
                    {
                        this.ProgressText = "Started to Shred File " + MdlShared.FilePath + " Please Wait.";
                        wiper.WipeFile(MdlShared.FilePath, 2);
                    }
                }

                this.ProgressIndex++;
            }
            if (btnShredRecycleBinEnable)
            {
                this.ProgressText = "Started to Shreding recyclebin please Wait.";

                Thread.Sleep(2000);
                foreach (string file in Directory.EnumerateFiles("C:\\$RECYCLE.BIN", "*.*", SearchOption.AllDirectories)
                    )
                {
                    this.ProgressText = "Started to Shred File " + file + " please Wait.";
                    wiper.WipeFile(file, 2);
                }
            }


            btnShreddingEnable = true;
            this.ProgressText = "Shredding is done.";
        }