shan
shan

Reputation: 1371

Reading and writing to files

I have a few questions. Yes this is homework and I am just trying to understand it.

This is what is being asked.

• When the button “Load” is clicked, read the file specified in the textbox (txtFilePath: Absolute path not relative) and add the objects found within to the listbox

• When the user clicks the “Save” button, write the selected record to the file specified in txtFilePath (absolute path not relative) without truncating the values currently inside

Can someone please explain to me as I am still learning this. I have the button and the textbox there and the same with the save. Now with the save button will I just have the same code as you would if you just wanted to save it. But from what I am gathering there is a database so you can load the file that you saved. Or am I making this harder than what it is?

Upvotes: 2

Views: 240

Answers (6)

jb.
jb.

Reputation: 10351

For Load:

  1. Read the file line by line
  2. Add each line to the ListBox Items

For Save:

  1. Open your save file without truncating (ie append to the file)
  2. For each item in your ListBox Items, write it to the save file

Upvotes: 1

JeremyS
JeremyS

Reputation: 329

First of all read up on how to read and write files. Here's a good link I found:

check it out

Next what you'll want to do is put your read/write code in the Button_Click event of each button (double click on your buttons to auto create this event assuming your using Visual Studio)

You can easily retrieve the path from your text box by accessing the .text() property of your textbox

string path = myTextBox.Text;

It's been a while since I've coded anything in c# but this is pretty basic and I think it should help.

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124790

No, no database. In these instructions, record == some selected item that needs to be appended to an existing file. Just use a stream and a writer to save the file to disk and you satisfy the requirement.

Upvotes: 2

Tormod
Tormod

Reputation: 4583

If I understand you correctly, your question is wether or not you need to read the file a second time before saving or otherwise treat if differently than if you created a new file.

I would say "no". You have already read the content of the file into the listbox. You just need to get the edited content from the listbox (when the user is done with it) and save it to the file (overwriting whatever is there).

Upvotes: 1

Saintt Sheldon Patnett
Saintt Sheldon Patnett

Reputation: 1163

It's hard to give a detailed analysis of this subject as it is quite a wide topic.

For file interaction you must use the System.IO namespace which has classes to easily load and save files.

http://msdn.microsoft.com/en-us/library/system.io.aspx

The link above is a good reference on MSDN on how you can get started with File Management using System.IO.

Good luck!

Upvotes: 1

kprobst
kprobst

Reputation: 16651

No, there is no database. What you do is interact with the Windows file system (eg, the files on your hard drive). You use the classes in the System.IO namespace to load and save files.

'Absolute path' refers to the unique location of a file in the drive expressed as a rooted expression; a 'relative path' is a partial path that points to a file relative to a given location:

c:\foo\bar\baz\my files\homework.txt
..\..\homework.txt

Those are an absolute and relative paths.

I'm not sure how much detail you are looking for here, it's hard to give a complete overview of the way filesystems work. You might want to look at the basic examples in MSDN that deal with file management.

Upvotes: 1

Related Questions