如何从文件中读取项目到项目列表中,并将属性设置为文件中的值? [英] how to read items from file into a list of items and set the properties to the value in the file?

查看:68
本文介绍了如何从文件中读取项目到项目列表中,并将属性设置为文件中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在尝试使用c#winforms创建一个库存日志系统,我对如何将这些项目读回到列表并将数据存储到属性中的想法有些困惑.

Hello I am trying to create a stock log system in c# winforms and i am a bit stuck for ideas on how to read the items back into a list and storing the data into the properties.

我将从一个csv文件中读取内容,其中每一行都是1项,每个属性都用逗号分隔.

I will be reading in from a csv file where each line is 1 item and each property is separated by a comma.

主要班级

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.IO;
//using Microsoft.VisualBasic;

namespace stock_list
{
    public partial class Form1 : Form
    {

        private List<item> itemlist = new List<item>((1));

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            saveitem(Convert.ToInt64(txtStallNumber.Text), Convert.ToInt64(txtStockNumber.Text), txtDescription.Text, Convert.ToDecimal(txtPaidprice.Text), Convert.ToDecimal(txtSoldPrice.Text));
        }

        private void btnItems_Click(object sender, EventArgs e)
        {
            readfromfile();
        }

        private void readfromfile()
        {
            var reader = new System.IO.StreamReader(@"file.csv", Encoding.UTF8, false);
            while (!reader.EndOfStream)
            {
                //what todo here??
            }
        }

        private void saveitem(long stallnumberpar, long stocknumberpar, string itemdiscriptionpar, decimal boughtpricepar, decimal soldpricepar, decimal profitorlosspar = 0)
        {
            itemlist.Add(new item { stallnumber = stallnumberpar, stocknumber = stocknumberpar, itemdescription = itemdiscriptionpar, boughtprice = boughtpricepar, soldprice = soldpricepar, profitorloss = soldpricepar - boughtpricepar});
            txtDescription.Clear();
            txtPaidprice.Clear();
            txtSoldPrice.Clear();
            txtStallNumber.Text = "";
            txtStockNumber.Clear();
            txtStallNumber.Focus();
            MessageBox.Show("Item Saved");
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            RunQueryDescription(Microsoft.VisualBasic.Interaction.InputBox("Enter Search Criteria", "Enter Search Criteria", "Default",0,0));
        }

        private void RunQueryDescription(string description)
        {
            //List<item> products = new List<item>((1));
            var writer = new System.IO.StreamWriter(@"file.csv", true, Encoding.UTF8);
            item[] productsarr = new item[itemlist.Count];
            int index = 0;
            foreach (item product in itemlist)
            {
                if (product.itemdescription.Contains(description))
                {
                    productsarr[index] = product;
                    index++;
                }
                else
                {
                    index++;
                    continue;
                }
            }
            for (int i = 0; i < productsarr.Length; i++)
            {
                MessageBox.Show(productsarr[i].stallnumber.ToString() + 
                    productsarr[i].stocknumber.ToString() + 
                    productsarr[i].itemdescription.ToString() + 
                    productsarr[i].boughtprice.ToString() + 
                    productsarr[i].soldprice.ToString() + 
                    productsarr[i].profitorloss.ToString());

                writer.Write(productsarr[i].stallnumber.ToString() + "," +
                    productsarr[i].stocknumber.ToString() + "," +
                    productsarr[i].itemdescription.ToString() + "," +
                    productsarr[i].boughtprice.ToString() + "," +
                    productsarr[i].soldprice.ToString() + "," +
                    productsarr[i].profitorloss.ToString());
                writer.Close();
                writer.Dispose();
            }

        }
    }
}

items类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

    namespace stock_list
    {
        class item
        {
            public long stallnumber { get; set; }
            public long stocknumber { get; set; }
            public string itemdescription { get; set; }
            public decimal boughtprice { get; set; }
            public decimal soldprice { get; set; }
            public decimal profitorloss { get; set; }

        }
    }

示例文件

1,1,Vase,1.00,2.00,1.00

任何帮助都将得到重视

预先感谢!

推荐答案

您可以使用File.ReadAllLines读取文件

private List<item> itemlist = new List<item>();
private void readfromfile()
{
    var lines = System.IO.File.ReadAllLines("path");

    foreach (string item in lines)
    {
        var values = item.Split(',');
        itemlist.Add(new item()
        {
            stallnumber = long.Parse(values[0]),
            stocknumber = long.Parse(values[1]),
            itemdescription = values[2],
            //and so on
        });
    }
}

这篇关于如何从文件中读取项目到项目列表中,并将属性设置为文件中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆