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
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. :)
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.
Re: Streamwriter creates file, but is 0 bytes
You arent writing anything.
Edit: Too late as usual
Re: Streamwriter creates file, but is 0 bytes
Wow, I beat someone to the post.
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.
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! :)
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.
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.
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.
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.