Click here to Skip to main content
15,923,374 members
Home / Discussions / C#
   

C#

 
GeneralRe: Completely delete a file Pin
Johan Martensson3-Jan-08 13:59
Johan Martensson3-Jan-08 13:59 
GeneralRe: Completely delete a file Pin
PIEBALDconsult3-Jan-08 8:55
mvePIEBALDconsult3-Jan-08 8:55 
GeneralRe: Completely delete a file Pin
PIEBALDconsult3-Jan-08 16:18
mvePIEBALDconsult3-Jan-08 16:18 
GeneralRe: Completely delete a file Pin
Johan Martensson3-Jan-08 23:29
Johan Martensson3-Jan-08 23:29 
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 
So I have cleaned up the code a bit and created a fixed buffer instead (which I should have done from the start Smile | :) )

The wiping of the entire last sector works fine, a text-file that was 18 bytes showed up as 512 bytes when recovered, with garbage data in it of course, so thanks for that one.
But then again, having the lines below before deleting the file, it shows up as 0 bytes when recovered.

FileStream tmpStream = new FileStream(filename, FileMode.Create);
tmpStream.Close();


Now I only have to get rid of the filename that shows up when trying to recover the file.
I did try to rename it but that didn't work, I think it has to do with the fact that it's moved, is there really no other way of renaming files in C#?

Current code:
private void WipeFile(string filename, int timesToWrite)
{
    if(File.Exists(filename))
    {
        byte[] dummyBuffer = new byte[512];
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        rng.GetBytes(dummyBuffer);

        double filelength = new FileInfo(filename).Length;
        double sectors = System.Math.Ceiling(filelength / 512);

        for (int timesWritten = 0; timesWritten < timesToWrite; timesWritten++)
        {
            FileStream inputStream = new FileStream(filename, FileMode.Open, FileAccess.Write, FileShare.None);                                         

            int sectorsWritten = 0;
            while (sectorsWritten < sectors)
            {
                inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
                rng.GetBytes(dummyBuffer);
                sectorsWritten++;
            }

            inputStream.Close();
        }

        FileStream tmpStream = new FileStream(filename, FileMode.Create);
        tmpStream.Close();

        File.Delete(filename);
    }
}


http://johanmartensson.se - Home of MPEG4Watcher

modified on Friday, January 04, 2008 6:33:34 PM

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 
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 

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.