Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1132
  • Last Modified:

Value does not fall within the expected range vb.net

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)
                K.Close()

Open in new window

0
JustAskMe
Asked:
JustAskMe
  • 6
  • 5
1 Solution
 
x77Commented:
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)
0
 
JustAskMeAuthor Commented:
Ok that works, Thanks
Now I have another problem... I want NumBytesRead to be longer then  bytes, is that possible?
0
 
x77Commented:
You can write aditional bytes (Spaces) using WriteByte.
Dim Space as byte = cbyte(Ascw(" "))
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JustAskMeAuthor Commented:
Thanks that helped a lot!

One more thing...

can I write a enter too? I want him to be on two lines
0
 
x77Commented:
Yes yo have ControlChars.cr, Lf , Also EnvironMent.NewLine ...
0
 
JustAskMeAuthor Commented:
Do I need to convert it?
0
 
x77Commented:
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)
0
 
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
0
 
x77Commented:
In this case, you must read all lines, update lines in memory, then write whole file.
0
 
JustAskMeAuthor Commented:
Oh this is tow much overhead...

Is it possible to add the line without overwriting the data in the file?
0
 
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now