Sequential text file

Joined
Dec 14, 2006
Messages
5
Programming Experience
1-3
I am supposed to store some data in a sequential text file that is read when my VB.NET program starts. I have to make sure that the text file will open on anyone's computer (as long as it is stored with the program). I stored it in the bin folder that contains my program, but when I run my program, it says that it cannot access the text file because it is being used by another process. I don't know what to do to make sure that the file will be opened on any computer as long as it is saved with the program when my program doesn't allow me to save it in the bin folder.
 
Can you post your code? My guess is that your opening the file multiple times within a loop and causing the problem.
 
Problem resolved, new problem below

I figured out my problem. I was trying to follow along in my book, and somehow I used File.OpenText and File.AppendText for the same filename, which I believe caused the problem. All I really needed was File.OpenText to read the file so I can copy each line to a list box.

But now I'm having a new problem. Every other line in the text file is a number, and I'm trying to use the Format function to put it in the system-defined currency format ("C") when I add it to the list box. (Every 2 consecutive lines in the text file end up on one line in the list box). Unfortunately, "C" is being displayed instead of the number in currency format.

Below is my code:

PublicSubNew()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call

Dim aFile As StreamReader
Dim LineofText AsString
Dim OutputFile As StreamWriter

aFile = File.OpenText(Application.StartupPath & "\Cars.txt")

While aFile.Peek() <> -1
LineofText = aFile.ReadLine()
LineofText &= " " & Format(aFile.ReadLine(), "C")
lstCars.Items.Add(LineofText)
EndWhile

aFile.Close()

EndSub
 
VB.NET:
[SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] aFile.Peek() <> -1[/SIZE]
  [SIZE=2]LineofText = aFile.ReadLine()[/SIZE]
  [SIZE=2]LineofText &= " " & FormatCurrency(aFile.ReadLine())[/SIZE]
  [SIZE=2]lstCars.Items.Add(LineofText)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE]
 
Back
Top