473,327 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Completely delete a file

I've been playing around with the concept of wiping files from a thumbdrive and it seems to be working pretty well.

I have the code below and the file seems to be overwritten as they should.
I have tested Ontrack EasyRecovery and PC Inspector File Recovery and I wasn't able to get any viewable data back.

The only annoying thing is that booth recovery-applications were able to bring back the filename of the overwritten file which I don't want of course.

If I don't rename the file before overwriting it, all that is recovered is a zero-byte file with the correct filename.
If I rename it first, I can recover a file with the correct filename and size but the data is destroyed.

So what I'm asking for is, is there a way to completely delete a file or change the name of the file permanently so it can't be recovered.


Expand|Select|Wrap|Line Numbers
  1. private void WipeFile(string filename, int timesToWrite)
  2. {
  3.     if(File.Exists(filename))
  4.     {
  5.         // Rename the file
  6.         string newFileName = Path.GetDirectoryName(filename) + GetRandomFileName();
  7.         File.Move(filename, newFileName);
  8.         filename = newFileName;
  9.  
  10.         // Create a buffer for the dummy-data
  11.         byte[] dummyBuffer = new byte[new FileInfo(filename).Length];
  12.         RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  13.         // Fill the buffer with random data
  14.         rng.GetBytes(dummyBuffer);
  15.  
  16.         // Write to the file n times
  17.         for (int timesWritten = 0; timesWritten < timesToWrite; timesWritten++)
  18.         {
  19.             FileStream inputStream = new FileStream(filename, FileMode.Create);
  20.             inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
  21.             // Get some new dummy-data
  22.             rng.GetBytes(dummyBuffer);
  23.             inputStream.Close();
  24.         }
  25.  
  26.         // Create a zero-byte file
  27.         FileStream tmpStream = new FileStream(filename, FileMode.Create);
  28.         tmpStream.Close();
  29.  
  30.         File.Delete(filename);
  31.     }
  32. }
Jan 3 '08 #1
3 1321
balabaster
797 Expert 512MB
I'm not sure what the purpose for your project is, but in case you're unintentionally reinventing the wheel, there are freeware file shredders out there that would suit this purpose.

I don't know if this helps or not...
http://www.bestfreewaredownload.com/...-xxxwfiid.html
Jan 3 '08 #2
Yes, I am in a way reinventing the wheel but I need this function within a project
Jan 3 '08 #3
balabaster
797 Expert 512MB
Yes, I am in a way reinventing the wheel but I need this function within a project
Okay, I'll keep my eyes open...my first thought is that you would need to find a way of modifying the FAT/NTFS at the lowest possible level because I think (somewhat like unix) the file table works in a somewhat similar manner as the file system...the file in the file table is marked for deletion and consequently isn't displayed, but is still physically there. So you'd need to actually remove the item from the FAT/NTFS. I haven't looked into this before though, this is just my understanding from using file shredders and tools like R-Studio.
Jan 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
5
by: Jobs | last post by:
Hello All, I want to delete all files in a directory. I am making a backup copy of all files in the directories say c:\abc by reading and writing to a file. After making a backup copy I want to...
0
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
2
by: Jacky Luk | last post by:
Hi, I can't seem to find a way to delete all records of a table. I created my tables in MySQL Query Browser, then fill them up with VC++, but some records were incorrect, And I had to recreate the...
2
by: Robert Kersten | last post by:
Hi everyone. I need to completely remove all entries in a number of tables. Dropping the tables works fine - my only problem is, that in production environment I don't have the right to drop...
12
by: Lucas Tam | last post by:
I have a very simple loop: If (Directory.Exists(tempDirectory)) Then Try Dim Files() As String = Directory.GetFiles(tempDirectory) 'Clear out directory For Each Filename As String In Files...
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
4
by: =?Utf-8?B?RWQ=?= | last post by:
I would like to COMPLETELY reset my computer ie. Send it back to it's state when i turned it on for the first time. Delete files and uninstall programs. I have already backed up all important...
0
by: teneesh | last post by:
I created a form in Access to store and retrieve data from a sql server table. When i go and try to add a record to the form - i get the error: Unique Table is nonexistent or not completely...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.