|
-
Feb 25th, 2009, 03:15 PM
#1
Thread Starter
Fanatic Member
Streamwriter creates file, but is 0 bytes
I am using the following code to write and empty (dummy) file to take up the remaining space on a device.
RemSpace is a Public Variable declared as Long
I have a function that gets the remaining space on the device and stores in the RemSpace variable (in bytes).
I have tested with a msgbox to display the result of RemSpace, and it comes back correct (not 0). However, when the following code attempts to create the dummy file, the file is properly created but is only 0 bytes. What am I missing here? Thanks for any clarification.
Code:
'Create the dummyfile to take up the remaining space on the drive
Dim myStreamWriter As New IO.StreamWriter("F:\dummyfile.txt")
For i As Integer = 1 To RemSpace
myStreamWriter.Write("")
Next
myStreamWriter.Close()
myStreamWriter.Dispose()
myStreamWriter = Nothing
-
Feb 25th, 2009, 03:17 PM
#2
Thread Starter
Fanatic Member
Re: Streamwriter creates file, but is 0 bytes
I feel inclined to clarify that this piece of code is not for malicous purposes. I am simply wanting to create a dummy file to take up the remaing space on a flash drive. Thanks.
-
Feb 25th, 2009, 03:18 PM
#3
Re: Streamwriter creates file, but is 0 bytes
You are writing no character over and over again. What did you expect the length to be? Nothing is being written many times, but N * 0 = 0 for all N.
My usual boring signature: Nothing
 
-
Feb 25th, 2009, 03:18 PM
#4
Re: Streamwriter creates file, but is 0 bytes
You arent writing anything.
Edit: Too late as usual
-
Feb 25th, 2009, 03:20 PM
#5
Re: Streamwriter creates file, but is 0 bytes
Wow, I beat someone to the post.
My usual boring signature: Nothing
 
-
Feb 25th, 2009, 03:24 PM
#6
Thread Starter
Fanatic Member
Re: Streamwriter creates file, but is 0 bytes
Ok, that makes sense now, but I thought it was possible to create an empty file that just takes up space.
-
Feb 25th, 2009, 03:34 PM
#7
Thread Starter
Fanatic Member
Re: Streamwriter creates file, but is 0 bytes
RemSpace in the StreamWriter code should be specified in bytes correct? I just built a 837 mb dummy file!
-
Feb 25th, 2009, 03:36 PM
#8
Re: Streamwriter creates file, but is 0 bytes
I would fill with a dummy variable. There may well be a way to make an arbitrarily sized file without setting the bytes to anything, but that would probably require some API code, and possibly more low level than that.
My usual boring signature: Nothing
 
-
Feb 25th, 2009, 04:44 PM
#9
Thread Starter
Fanatic Member
Re: Streamwriter creates file, but is 0 bytes
I'm now using VB.Net's Random class to generate random numbers to fill the dummy file with.
-
Feb 25th, 2009, 05:56 PM
#10
Re: Streamwriter creates file, but is 0 bytes
That's overkill, in my opinion, but there are some advantages to it. For one thing, anybody looking at the file in a binary mode will see what appears to be meaningful information, but that point will be missed by everybody else. Just filling with a single character, such as space, or an asterisk, would be faster, but it would also clearly be filler if somebody opened the file in a binary reader.
My usual boring signature: Nothing
 
-
Feb 25th, 2009, 06:10 PM
#11
Re: Streamwriter creates file, but is 0 bytes
Also thought I'd add that you can use:
My.Computer.FileSystem.WriteAllText("FILENAME", Text, False)
Which will be less code than using streamwriter. Just create a string variable to hold the random numbers and then replace Text with the variables name. False is for whether or not you want to append the text to the file (add) or just replace the text already in the file. False will replace the text already in the file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|