Click here to Skip to main content
15,922,894 members
Home / Discussions / C#
   

C#

 
GeneralRe: Completely delete a file [modified] Pin
PIEBALDconsult4-Jan-08 5:51
mvePIEBALDconsult4-Jan-08 5:51 
GeneralRe: Completely delete a file Pin
Johan Martensson4-Jan-08 7:09
Johan Martensson4-Jan-08 7:09 
GeneralRe: Completely delete a file Pin
PIEBALDconsult4-Jan-08 10:03
mvePIEBALDconsult4-Jan-08 10:03 
GeneralRe: Completely delete a file [modified] Pin
Johan Martensson4-Jan-08 12:27
Johan Martensson4-Jan-08 12:27 
GeneralRe: Completely delete a file Pin
PIEBALDconsult4-Jan-08 16:13
mvePIEBALDconsult4-Jan-08 16:13 
GeneralRe: Completely delete a file Pin
Johan Martensson4-Jan-08 21:48
Johan Martensson4-Jan-08 21:48 
GeneralRe: Completely delete a file Pin
PIEBALDconsult5-Jan-08 4:47
mvePIEBALDconsult5-Jan-08 4:47 
GeneralRe: Completely delete a file Pin
Johan Martensson6-Jan-08 11:51
Johan Martensson6-Jan-08 11:51 
After a little digging I found the FileStream.SetLength Method which truncates the file if the given value is less than the current length.

After using the code below, when trying to recover the file, I end up with a zero-byte file with the dates set to January 2037 and the space on the disc where the file used to be is filled with garbage.

So what's accomplished now is, the file is overwritten with garbage data n times, the original filesize and dates removed.

Even though the filename will keep haunting me and I'll keep looking for a solution, I believe the file is pretty much wiped out and can't be recovered with any standard methods.

private void WipeFile(string filename, int timesToWrite)
{
    if(File.Exists(filename))
    {
        File.SetAttributes(filename, FileAttributes.Normal);

        double sectors = System.Math.Ceiling(new FileInfo(filename).Length / 512.0);
        byte[] dummyBuffer = new byte[512];

        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        rng.GetBytes(dummyBuffer);

        FileStream inputStream = new FileStream(filename, FileMode.Open);
        for (int timesWritten = 0; timesWritten < timesToWrite; timesWritten++)
        {
            inputStream.Position = 0;
            int sectorsWritten = 0;
            while (sectorsWritten < sectors)
            {
                inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
                rng.GetBytes(dummyBuffer);
                sectorsWritten++;
            }
        }
        inputStream.SetLength(0);
        inputStream.Close();

        DateTime dt = new DateTime(2037, 1, 1, 0, 0, 0);
        File.SetCreationTime(filename, dt);
        File.SetLastAccessTime(filename, dt);
        File.SetLastWriteTime(filename, dt);

        File.Delete(filename);
    }
}


http://johanmartensson.se - Home of MPEG4Watcher

GeneralRe: Completely delete a file Pin
PIEBALDconsult6-Jan-08 13:45
mvePIEBALDconsult6-Jan-08 13:45 
GeneralRe: Completely delete a file Pin
Johan Martensson7-Jan-08 1:12
Johan Martensson7-Jan-08 1:12 
GeneralCalling InitializeFromBitmap fails with exception "The method or operation is not implemented" Pin
KeesVer3-Jan-08 2:29
KeesVer3-Jan-08 2:29 
GeneralRe: Calling InitializeFromBitmap fails with exception "The method or operation is not implemented" Pin
KaptinKrunch3-Jan-08 16:29
KaptinKrunch3-Jan-08 16:29 
GeneralRe: Calling InitializeFromBitmap fails with exception "The method or operation is not implemented" Pin
KeesVer3-Jan-08 23:31
KeesVer3-Jan-08 23:31 
Questionopen an associated filetype Pin
peatle3-Jan-08 1:00
peatle3-Jan-08 1:00 
GeneralRe: open an associated filetype Pin
Phil J Pearson3-Jan-08 1:32
Phil J Pearson3-Jan-08 1:32 
GeneralRe: open an associated filetype Pin
Giorgi Dalakishvili3-Jan-08 1:39
mentorGiorgi Dalakishvili3-Jan-08 1:39 
GeneralWriting Objects into a Databases Pin
00carl003-Jan-08 0:50
00carl003-Jan-08 0:50 
GeneralRe: Writing Objects into a Databases Pin
darkelv3-Jan-08 0:57
darkelv3-Jan-08 0:57 
GeneralRe: Writing Objects into a Databases Pin
Giorgi Dalakishvili3-Jan-08 1:39
mentorGiorgi Dalakishvili3-Jan-08 1:39 
GeneralRe: Writing Objects into a Databases Pin
Russell Jones3-Jan-08 5:51
Russell Jones3-Jan-08 5:51 
GeneralRe: Writing Objects into a Databases Pin
Giorgi Dalakishvili3-Jan-08 7:59
mentorGiorgi Dalakishvili3-Jan-08 7:59 
GeneralRe: Writing Objects into a Databases Pin
Russell Jones3-Jan-08 5:55
Russell Jones3-Jan-08 5:55 
QuestionWhich one is good practice? Pin
Hum Dum3-Jan-08 0:00
Hum Dum3-Jan-08 0:00 
AnswerRe: Which one is good practice? Pin
Colin Angus Mackay3-Jan-08 0:49
Colin Angus Mackay3-Jan-08 0:49 
AnswerRe: Which one is good practice? Pin
Alaric_3-Jan-08 4:16
professionalAlaric_3-Jan-08 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.