简体   繁体   English

读写文件

[英]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 •单击“加载”按钮时,读取文本框中指定的文件(txtFilePath:绝对路径不相对),然后将找到的对象添加到列表框中

• 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 •当用户单击“保存”按钮时,将选定的记录写入txtFilePath中指定的文件(绝对路径不是相对路径),而不会截断当前内部的值

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? 还是我要比这更难?

No, no database. 不,没有数据库。 In these instructions, record == some selected item that needs to be appended to an existing file. 在这些说明中, record == some selected item需要添加到现有文件的some selected item Just use a stream and a writer to save the file to disk and you satisfy the requirement. 只需使用流和编写器将文件保存到磁盘,即可满足要求。

No, there is no database. 不,没有数据库。 What you do is interact with the Windows file system (eg, the files on your hard drive). 您要做的是与Windows文件系统(例如,硬盘驱动器上的文件)进行交互。 You use the classes in the System.IO namespace to load and save files. 您可以使用System.IO命名空间中的类加载和保存文件。

'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. 您可能需要查看MSDN中处理文件管理的基本示例。

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) 接下来,您要做的是将您的读/写代码放入每个按钮的Button_Click事件中(如果您使用Visual Studio,请双击您的按钮以自动创建此事件)

You can easily retrieve the path from your text box by accessing the .text() property of your textbox 您可以通过访问文本框的.text()属性轻松地从文本框中检索路径。

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. 自从我用c#编写代码以来已经有一段时间了,但这是非常基本的,我认为应该会有所帮助。

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. 对于文件交互,您必须使用System.IO命名空间,该命名空间具有可轻松加载和保存文件的类。

http://msdn.microsoft.com/en-us/library/system.io.aspx 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. 上面的链接是MSDN上有关如何使用System.IO入门文件管理的很好的参考。

Good luck! 祝好运!

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). 您只需要从列表框中获取编辑的内容(当用户完成操作时)并将其保存到文件中(覆盖其中的任何内容)。

For Load: 对于负载:

  1. Read the file line by line 逐行读取文件
  2. Add each line to the ListBox Items 每一行添加到ListBox项目

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 对于列表框项目中的每个项目,将其写入保存文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM