c# read/write to a file.

  • Thread starter Thread starter lltaylor
  • Start date Start date
L

lltaylor

Hello All,

I apologise for such a basic question, I am just getting to grips with
c#, I usually develop in java.

What I am trying to do is:

I have a file which stores look-up information, as part of the
functionality of my application, I allow the user to change the lookup
data. Whats the best method in c# for editing a file.

For example:
I am trying to insert 10 lines at say line 50 of my file,
or another eg is I am trying to edit line 25 of my file??

Apologies again for such a basic question, any help on this matter
would be gratefully recieved.

Lloyd
 
lltaylor,

If the file uses a text format, then you will want to use a StreamReader
(with the proper encoding) to read the file. If each line has a
fixed-length layout, then you can easily skip ahead to any line by just
reading the appropriate number of characters. This would probably be faster
because it would perform a seek on the underlying stream. However, if the
size of the lines varies, then you will just want to call ReadLine the
appropriate number of times.

If you are only moving forward, then this should be fine. However, if
you need to move randomly in your file, then you will want to access the
BaseStream property on your reader and call Seek on it to move an
appropriate number of places. Then, you will have to call
DiscardBufferedData on the reader so that the reader knows that you have
reset the position in the underlying stream.

Thinking about it now, it is probably better off if you just use the
BaseStream exposed by the StreamReader, as it will unify the code for
navigation.

Hope this helps.
 
lltaylor said:
Hello All,

I apologise for such a basic question, I am just getting to grips with
c#, I usually develop in java.

What I am trying to do is:

I have a file which stores look-up information, as part of the
functionality of my application, I allow the user to change the lookup
data. Whats the best method in c# for editing a file.

For example:
I am trying to insert 10 lines at say line 50 of my file,
or another eg is I am trying to edit line 25 of my file??

Apologies again for such a basic question, any help on this matter
would be gratefully recieved.

Lloyd

If multiple threads (users) needs to have write-access to some datastore,
wouldn't it be better to store that data in a database?

As far as I know it is not possible (in any system) to add lines in the
middle
of a textfile, especially when other threads are changing other parts of
that file.

Hans Kesting
 
lltaylor said:
I apologise for such a basic question, I am just getting to grips with
c#, I usually develop in java.

What I am trying to do is:

I have a file which stores look-up information, as part of the
functionality of my application, I allow the user to change the lookup
data. Whats the best method in c# for editing a file.

For example:
I am trying to insert 10 lines at say line 50 of my file,
or another eg is I am trying to edit line 25 of my file??

Apologies again for such a basic question, any help on this matter
would be gratefully recieved.

You can't insert or remove anything in a file (in any language) without
reading the rest of the file and writing it out again, basically. You
could either do this by reading the whole file into memory at once and
writing it out again after making the changes (eg using an ArrayList
with each line being an element) or by reading from the file and
writing to a temporary new file, inserting the new stuff as you go, and
then renaming the file afterwards.
 
Back
Top