Value does not fall within the expected range

I try to overwrite a line in a text file using stream writer

but I get this error:

value does not fall within the expected range
Dim K As New IO.FileStream(ItemFileName, IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.ReadWrite)
                '  K.Seek(P, IO.SeekOrigin.Begin)

                Dim value As String = Regex.Replace(s, SS(ItemFields.PcCost), newValue)

                NumBytesRead = System.Text.ASCIIEncoding.Unicode.GetBytes(value).Length

                Dim bytes() As Byte = System.Text.ASCIIEncoding.Unicode.GetBytes(value)

                K.Write(bytes, P, NumBytesRead)

Open in new window

Who is Participating?
In this case, you must read all lines, update lines in memory, then write whole file.
I think P is offset on file.

FileStream..::.Write (Método)
Public Overrides Sub Write (       array As Byte(),       offset As Integer,       count As Integer )

In this case, offset is offset in array.
You need Seek to position the filestream.    K.Seek(P, IO.SeekOrigin.Begin)
JustAskMeAuthor Commented:
Ok that works, Thanks
Now I have another problem... I want NumBytesRead to be longer then  bytes, is that possible?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

You can write aditional bytes (Spaces) using WriteByte.
Dim Space as byte = cbyte(Ascw(" "))
JustAskMeAuthor Commented:
Thanks that helped a lot!

One more thing...

can I write a enter too? I want him to be on two lines
Yes yo have, Lf , Also EnvironMent.NewLine ...
JustAskMeAuthor Commented:
Do I need to convert it?
To avoid convert it, use BinaryWriter.

Dim Bw as new BinaryWriter(MyStream)

Then You Can Write Directly String.Arrays, Use Writeline etc..
note, When use BinaryWriter, and Write Strings, it Write a 7BitEncoded Prefix with String.Length, to avoid this
use Write(MyString.ToCharArray)
JustAskMeAuthor Commented:
Ok thanks for this tip!

But I see my problem is not solved :(

I have a file with a lot of lines like that:

sample line 1
sample line 2
sample line 3

now I want to overwrite just one line with a new value.

now some times the new value is longer then the old one, here's were the problem is...

what it does it itt overwrites the secound line like this:

New sample line 1le line 2
sample line 3

that's a mess!

Any idea how to overcome that?

Thanks a lot for your help
JustAskMeAuthor Commented:
Oh this is tow much overhead...

Is it possible to add the line without overwriting the data in the file?
JustAskMeAuthor Commented:
Bassiclly what I want to do now is that:

I'll write the line and then add a space but that space shouled not overrite the bytes in the txt, but push up a bit all the data to make place for him
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.