Results 1 to 11 of 11

Thread: Streamwriter creates file, but is 0 bytes

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    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

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    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.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Streamwriter creates file, but is 0 bytes

    You arent writing anything.


    Edit: Too late as usual

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Streamwriter creates file, but is 0 bytes

    Wow, I beat someone to the post.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    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.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    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!

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    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.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  11. #11
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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
  •  



Click Here to Expand Forum to Full Width