Words on my addictions: .NET, Philosophy, Music

20 May 2005

Secure Deletion of files

Filed under: C# — stefanprodan @ 1:09 pm

Here it is a safe method based on the .NET Random class:

/// <summary> 
/// Secure Deletion method 
/// </summary> 
/// <param name="strFilename"> 
/// the path of the file to be erased 
/// </param> 
/// <param name="nTimes"> 
/// number of times to run the erase algorithm 
/// </param> 
public static void EraseFile(string strFilename, int nTimes) 
{ 
    if (!File.Exists(strFilename)) 
    { 
        throw new 
              ArgumentException("The file does not exist"); 
    } 
    while (nTimes >= 0) 
    { 
        FileStream fs = File.OpenWrite(strFilename); 
        int nBytesInFile = (int)fs.Length; 

Random random = new Random(); 
        Byte[] b = new Byte[nBytesInFile]; 
        random.NextBytes(b); 

for (int i = 0; i < nBytesInFile; i++) 
        { 
            fs.WriteByte(b[i]); 
        } 
        fs.Close(); 

nTimes--; 
    } 
}

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: