Best Method of writing large volumes of nothing?

joe_pool_is

Well-known member
Joined
Jan 18, 2004
Messages
451
Location
Texas
We have been receiving a lot of high capacity flash drives with bad memory in the upper parts of the drive. For a 16 GB flash drive, most would probably never notice it, but once you do, the drive may become unreadable and all the data is gone!

One way we have been testing the drives is by copying just under 16 GB of photos and MP3s to it, then deleting them back off.

But Im a Developer! I want to develop something to do this instead!

I created a simple form with a start button. When the button is clicked, there is a simple loop inside a Try statement that writes data using a StreamWriter object:
Code:
Dim writer As New StreamWriter("F:\\filler.dat")
Try
  While (True)
    writer.Write("Red Rum! ")
  End While
Catch ex As Exception
Finally
  writer.Close()
End Try
The theory was, once the flash drive runs out of memory space, an Exception would be thrown within the Try block and the Finally block would neatly close the file.

The problem was, it ran for about 2 hours last night, and wasnt even up to 1-GB yet.

Yuck!

Is there a better, faster way to accomplish my objective?
 
Yes it will take along time to do the way you do it. I know this as we do log files for our automated test for days and weeks. I would make a file around 300 to 500 megabytes, then "copy as" to the file until you reach close to 16 meg. Or using a photo that is high in bytes. Then "save as" Pic1, Pic2, etc,,,,

The picture way would be better I think now that I think about it.
 
If you use a filestream and just write large blocks from a large byte array it should go just about as fast as anything. For example,


C# Code[HorizontalRule]magic hidden text[/HorizontalRule]FileStream fs = new FileStream("E:\\test.txt", FileMode.Create, FileAccess.Write);

byte[] bigArray = new byte[1024 * 1024*4]; // 4 megs
for(int i = 0; i < 256; i++) { // Write 1 gig
 
I probably should have noticed that you used VB and not C# in your posted code, and posted my example in VB. Sorry.
 
Oh, its no biggie. I use both, but generally show my examples in VB so more people can read my code. I actually prefer C#.
 
Back
Top