文件路径和文本框
本文关键字:文本 路径 文件 | 更新日期: 2023-09-27 18:32:03
好的,我要再试一次。我现在得到了更多信息。我知道我不能使用打开和保存对话框,也没有数据库。所以我仍然有点迷茫,因为我之前被展示了如何使用打开和保存对话框来做到这一点。我将把我应该做的事情,然后到目前为止我拥有的代码。我拥有的代码也必须构建和添加。我还将展示我应该添加的内容。我只是想找到最好的方法来理解这个原因,现在我不是。我仍然是新手,我知道最近几天人们一直在试图帮助我理解,然后我被告知它不是打开和保存对话框。这是我应该做的。
•添加一个名为 txtFilePath 的文本框<---该文本框已有该
文本框•在上面的文本框旁边添加一个按钮,上面写着"加载"(适当命名)<- 已经有
•添加一个按钮,显示"保存"(适当命名) <- 已经有这个
•单击"加载"按钮时,读取文本框中指定的文件 (txtFilePath:绝对路径不是相对的)并添加找到的对象 在列表框中<--- 不理解
•当用户单击"保存"按钮时,将所选记录写入 在 txtFilePath 中指定的文件(绝对路径不是相对的),没有 截断当前内部的值<-- 不理解
这是我拥有的代码的一部分:'
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
EditDialog newEmployeeDialog = new EditDialog();
if (newEmployeeDialog.ShowDialog() == DialogResult.OK)
{
employeeList.Items.Add(newEmployeeDialog.StaffMember);
}
}
private void deleteButton_Click(object sender, EventArgs e)
{
if (employeeList.SelectedIndex == -1)
return;
if (MessageBox.Show("Really delete this employee",
"Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)
== DialogResult.Yes)
{
employeeList.Items.Remove(
employeeList.SelectedItem);
}
}
private void editButton_Click(object sender, EventArgs e)
{
if (employeeList.SelectedIndex == -1)
return;
int employeeNum = employeeList.SelectedIndex;
EditDialog newEmployeeDialog = new EditDialog();
newEmployeeDialog.StaffMember =
(Employee)employeeList.SelectedItem;
if (newEmployeeDialog.ShowDialog() == DialogResult.OK)
{
employeeList.Items.RemoveAt(employeeNum);
employeeList.Items.Insert(employeeNum, newEmployeeDialog.StaffMember);
employeeList.SelectedIndex = employeeNum;
}
}
private void employeeList_SelectedIndexChanged(object sender, EventArgs e)
{
if (employeeList.SelectedIndex != -1)
{
Employee currentEmployee = (Employee)employeeList.SelectedItem;
firstName.Text = currentEmployee.FirstName;
lastName.Text = currentEmployee.LastName;
jobTitle.Text = currentEmployee.JobTitle;
}
else
{
firstName.Text = lastName.Text = jobTitle.Text = "";
}
}
`
现在我知道你看不到按钮点击,但我确实有标记。我知道当您使用打开并保存它的工作方式时。我该怎么做?我会使用流编写器。我知道用户将在文本框中键入路径,当用户点击加载时,它将加载指定的文件。现在我只是想理解一个代码,以便能够正确地表达这一点。
会是这样的:
String filePath = this.txtFilePath.Text;
因为我需要命名文本框 txt文件路径.我知道你们中的一些人可能会说这很简单,但当你第一次学习它时,它似乎并不那么简单。自从我在家上大学以来,我一直在尝试一些东西来帮助我理解。感谢您的阅读,希望收到你们的来信。
更新:会是这样的吗
读取文件
private void Load_Click(object sender, EventArgs e)
{
StreamReader myReader = new StreamReader(C:''")
txtFilePath.Text = my reader.read to end();
myReader.Close();
}
然后是写入文件
{
StreamWriter myWriter = new StreamWriter("C:''test.txt", true);
myWriter.Write("Some text");
myWriter.WriteLine("Write a line");
myWriter.WriteLine("Line 2");
myWriter.Close();
}
如果这是正确的,那么我必须得到它,如果文件不存在,记事本弹出,以便他们可以添加它,然后他们可以保存它而不会删除文件或文件中的任何内容。
假设该文件包含员工姓名列表,您应该能够使用如下所示的内容将它们加载到列表框中:
var filepath = txtFilePath.Text;
if (File.Exists(filepath))
{
var lines = File.ReadAllLines(filepath);
foreach (var line in lines)
employeeList.Items.Add(line);
}
然后,假设您要将新的员工姓名添加到用户刚刚输入到列表框中的文件:
var filepath = txtFilePath.Text;
if (File.Exists(filepath))
using (var sw = File.AppendText(filepath))
sw.WriteLine((string)employeeList.Text);
else
using (var sw = File.CreateText(filepath))
sw.WriteLine((string)employeeList.Text);
这还没有经过测试,但它应该几乎按原样工作......