C# 如何使用GUI查看相同格式的数据

C# 如何使用GUI查看相同格式的数据,c#,C#,创建表单以输入将保存到文件中的库存项目。您需要创建一个类来包含项目代码、描述和价格。使用表单上的按钮将输入的项目添加/保存到文件中。这也将在添加竞争时清除表单上的文本框。这将为添加下一项做好准备。在表单上包含另一个按钮,当按下该按钮时,将在表单上显示文件(您添加的)中的所有项目 这就是我需要做的主题。我尝试了几种方法,但它总是崩溃,或给我一个消息,我的文件正在使用。你们能告诉我需要做什么来解决这个问题吗 public partial class Form1 : Form { co

创建表单以输入将保存到文件中的库存项目。您需要创建一个类来包含项目代码、描述和价格。使用表单上的按钮将输入的项目添加/保存到文件中。这也将在添加竞争时清除表单上的文本框。这将为添加下一项做好准备。在表单上包含另一个按钮,当按下该按钮时,将在表单上显示文件(您添加的)中的所有项目

这就是我需要做的主题。我尝试了几种方法,但它总是崩溃,或给我一个消息,我的文件正在使用。你们能告诉我需要做什么来解决这个问题吗

    public partial class Form1 : Form
{
    const string DELIM = ", ";
    const string FILENAME = @"C:\Users\Duckie\Desktop\C# Practice\Chapter.04\Quiz4\Inventory.txt";
    int code;
    string description;
    double price;
    static FileStream outFile = new
        FileStream(FILENAME,FileMode.Create,FileAccess.Write);
    StreamWriter writer = new StreamWriter(outFile);

    public Form1()
    {
        InitializeComponent();
    }

    private void saveButton_Click(object sender, EventArgs e)
    {
        code = Convert.ToInt32(codeBox.Text);
        description = descriptionBox.Text;
        price = Convert.ToDouble(priceBox.Text);
        writer.WriteLine(code + DELIM + description + DELIM + price);
        codeBox.Clear();
        descriptionBox.Clear();
        priceBox.Clear();
    }
}

}首先,使用SaveFileDialog打开文件。如果要在列表或表格中显示所有库存项目,请使用OpenFileDialog打开文件。然后,不需要在字符串变量中包含文件的完整路径。如果将文件移动到另一个目录,会发生什么情况

好的,这里有一些代码,可以保存到文本文件中,然后读取它。基本上,代码的作用是将您键入的名称保存在文本框中。唯一的要求是该文件已经存在(我只使用了OpenFileDialog)。 保存和读取文件的类:

class SaveToFile
{
    static List<string> nameList = new List<string>();
    public static void Save(string filePath, string input)
    {   ///true only says that it is okay to append new lines into the file
        using (StreamWriter sWriter = new StreamWriter(filePath, true))
        {
            sWriter.WriteLine(input);
        }
    }

    public static List<string> Read(string filePath)
    {
        string line = string.Empty;
        nameList.Clear();
        using (StreamReader sReader = new StreamReader(filePath))
        {
            while ((line = sReader.ReadLine()) != null)
            { 
                //I populate the list with the lines in the file
                nameList.Add(line);
            }
        }
        return nameList;
    }
 }

我希望这将帮助您了解如何写入和读取文件。

有时,异常并不能说明问题的完整性,可能是您在写入此目录时遇到权限问题,或者甚至该目录不存在,请将路径更改为简单的“c:\”,然后查看解决问题的方法。另外,尽量不要在路径中使用特殊字符,在目录名中使用空格,例如“C#Practice”@“C:\Inventory.txt?当然,如果这解决了问题,那么这是一个路径问题,然后更改代码以确保目录存在,如果不创建它,那么您就知道您有权限写入目录。@但您在这里也做了一些错误。
static FileStream outFile=new FileStream(文件名,FileMode.Create,FileAccess.Write);StreamWriter writer=newstreamwriter(outFile)
将他的代码放在一个方法中,使用谷歌等免费工具查看如何使用
SaveFileDialog和OpenFileDialog
在线上有大量的工作示例来描述如何使用它们我对C#还是个新手。这是我第一次接触所有这些代码,但我仍然不了解其中的一些内容。你能吗给我看一些例子?是的,我马上回来。谢谢你的帮助…如果一些类似于我需要编码的主题的东西会很棒。谢谢你的帮助…如何做非常清楚…所以我上面的代码根本没有用?或者我仍然可以用它作为我的保存按钮?
public partial class SaveReadForm: Form
    {
        private OpenFileDialog opnFileDialog = new OpenFileDialog();
        Person p = new Person();
        public SaveReadForm()
        {
            InitializeComponent();

            opnFileDialog.Filter = "Text File[.txt]|*.txt";

        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (opnFileDialog.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                if (!String.IsNullOrEmpty(input.Text))
                {
                    p.Name = input.Text;
                    SaveToFile.Save(opnFileDialog.FileName, p.Name);
                }
                else
                {
                    MessageBox.Show("Add your name", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void readBtn_Click(object sender, EventArgs e)
        {

            if (opnFileDialog.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                nameLstBox.Items.Clear();
                foreach (string s in SaveToFile.Read(opnFileDialog.FileName))
                {
                    nameLstBox.Items.Add(s);
                }
            }
        }