file should be only 1kb

bcm

Well-known member
Joined
Aug 13, 2007
Messages
56
Programming Experience
Beginner
I want to create a text file whose size should be only 1kb. The datashould not exceed more than that.
How can I do that in VB.NET?
:confused:
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
Give your threads relevant titles. Thread was given a relevant title.
VB.NET:
Dim bytes(999) As Byte
Dim fs As New IO.FileStream("1kbfile", FileMode.Create, FileAccess.Write)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
Dim bytes(1024) As Byte, surely!? ;)

But maybe easier something like (not syntax checked):

File.WriteAllText(Iif(myString.Length > 1024, myString.Substring(0,1024), myString), Encoding.ASCII)
I usually read small "k" as SI unit kilo (1000), large "K" as non-standardized legacy/computer kilo (1024). Also bytes(1024) would be 1025 bytes...

.Net 1 doesn't have a WriteAllText method.
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
I usually read small "k" as SI unit kilo (1000), large "K" as non-standardized legacy/computer kilo (1024).
Bwahahaah.. John, dang.. You kill me, you really do. Youre a proper funny guy, and I love your posts. Hopefully you'll find it a compliment to know I think of you like I do Data in Star Trek - hilarious and amazing because he's absolutely right on every occasion and works with infallible precision.

Also bytes(1024) would be 1025 bytes...
Indeed, that's one of those VB idiosyncracies (stupidities IMHO) that a C# programmer will forget quite easily..

.Net 1 doesn't have a WriteAllText method.
And I'm normally pretty good with that too. Good to know that when I'm wrong on the 1% of occasions, there's John as the backup.. :p You want a job? :)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
Indeed, that's one of those VB idiosyncracies (stupidities IMHO) that a C# programmer will forget quite easily..
It's the little differences... anyway OP should get the idea from both suggestions.
You want a job? :)
I'm fully booked for the time being, but thanks for offer ;)
 

rfaricy

Well-known member
Joined
Feb 9, 2006
Messages
46
Programming Experience
5-10
Yeah. Wouldn't an uppercase K be Kelvin? :p "KB" is the proper notation for kilobyte and a kilobyte is always 1024 bytes (never 1000). :)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
Ah.. therein lies the final nitpick; kb most definitely and resolutely means kilobits, regardless of what size we are attributing to the kilo part, we really should be advising the OP to make an array of Byte(124) or Byte(127) ;)
 
Top Bottom