This looks like the same code as mine. The issue I need to resolve is opening the file as read only.
Main Topics
Browse All TopicsI need some code that reads a file line by line that is compitable with VB 2003 (.NET 1.1).
I have been using the StreamReader however, I noticed than if Excel opens the file that my program is trying to read, the program crashes because it is locked. The System.IO.FileStream appears to have options to open the file in ReadOnly mode, thus hopefully ignoring any file locks from Excel.
I require some code to open a CSV file and read each line. The below example works however I need to ensure that my code does not lock a file during the stream or care about another program that opened the file.
Example:
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Try this
What kind of file are you trying to open?
If you're trying to open MS Office files in read-only mode (without changing file attributes), then you can do it by using Word ActiveX library.
- in Project -> References select Word x.x library (where x is a version number, mine is 9.0)
- paste this code:
'-------------------------
Option Explicit
Dim a As Word.Application 'define an object which is used to communicate with MS Word Application
Private Sub Form_Load()
Set a = New Word.Application 'create a Word Application Object (Start new Word Application)
a.Visible = True 'make it visibe
a.Documents.Open "c:\test.txt", False, True 'open the file in read-only mode (third parameter enables read-only mode)
End Sub
Private Sub Form_Unload(Cancel As Integer)
a.Quit 'quit Word
Set a = Nothing 'clean-up
End Sub
'-------------------------
This will start Word application and open C:\test.txt in read-only mode.
To open different types of office files, for example Excell or PowerPoint, you have to include corresponding library and open it in the same way as in
the example above.
http://www.experts-exchang
I have created a Windows Service that reads a simple text file. The extension of the text file is CSV. The CSV file has a list of server names, and the scheduled times for them to reboot. Each hour, each server checks this file. It seems when more than one server reads this file at the same time, the file gets locked. I am using StreamReader to do this.
Think out side the box kind of issue
I guess the work arround is to work with the schedule or the call to read the file.
What I mean is try to read the file one by one by each server or program, and avoid having two server reading the file at the same time.
Or create a script to clone\Create copies of the file if the file is being read it or lock.
hope this helps
Business Accounts
Answer for Membership
by: ranhellPosted on 2008-12-05 at 13:42:59ID: 23109120
try this code
Select allOpen in new window