简体   繁体   English

C#从文本文件中检索不同的数据类型数据并将其存储到变量

[英]C# retrieve different Data Types data from a text file and store it to variables

So what problem have i ran into - im trying to create a complex(complex for me) Bank management system. 因此,我遇到了什么问题-我试图创建一个复杂的(对我来说很复杂)的银行管理系统。 It takes various data from a person. 它从一个人那里获取各种数据。 It takes Strings and ints. 它需要字符串和整数。 I managed to store that data to a text file. 我设法将数据存储到文本文件中。

Text File looks Like this: 文本文件如下所示:

AccID: 74016 | 编号:74016 | NAME: anyname | NAME:任何名字| AGE: 18 | 年龄:18 | LASTNAME: anylastname | 姓氏:anylastname | BALANCE: 300 | 平衡:300 | ACCOUNT TYPE: Savings Account | 帐户类型:储蓄帐户|

(this data is in one row in a text file) (此数据在文本文件的一行中)

this is how i store it: 这是我存储的方式:

              var StoreInFile = new StreamWriter(@"C:\Accounts.txt", true);


                StoreInFile.Write("AccountID: " + AccID + " | ");
                StoreInFile.Write("NAME: " + _Name + " | ");
                StoreInFile.Write("AGE: " + _Age + " | ");
                StoreInFile.Write("LASTNAME: " + _LastName + " | ");
                StoreInFile.Write("BALANCE: " + Balance + " | ");
                StoreInFile.Write("ACCOUNT TYPE: " + accountType + " | ");
                StoreInFile.WriteLine(" ");
                StoreInFile.WriteLine(" ");

                StoreInFile.Close();

The problem now is how do i get that data from it? 现在的问题是如何从中获取数据? how do i store all those different strings/ints back to variables, where then i can manipulate them? 我如何将所有这些不同的字符串/整数存储回变量,然后在哪里可以操纵它们?

Ideally, i'd love to only input AccountID when logging in, and then it would automatically store all that Name, LastName, Age... and so on Data into variables back in the code, which are in that row. 理想情况下,我只想在登录时输入AccountID,然后它将所有Name,LastName,Age ...等数据自动存储到该行代码中的变量中。 Hope my question is clear. 希望我的问题清楚。

var properties = new prop();
var json = JsonConvert.SeralizeObject(properties);

public class prop 
{
     public string account_id { get; set;}
     public string name { get; set;}
     public int age { get; set;}

}

Something like this would help you a ton. 这样的事情会帮助你很多。

You can create a account class with properties you used: 您可以使用您使用的属性创建帐户类:

class Account
{
    public int Id { get; set;}
    public string Name { get; set;}
    public int Age{ get; set;}
    public string Lastname { get; set;}
    public int Balance { get; set;}
    public string AccountType { get; set;} // this is better to be enum but your example shows you have stored string
}

Then you can create an instance of Account class read the file split it with | 然后您可以创建一个Account类的实例,读取该文件,并使用| and just set its properties (parse to int where needed): 并设置其属性(在需要时解析为int):

string[] data = File.ReadAllText("filepath.txt").Split('|');
Account account = new Account();
foreach (string item in data)
{
    string[] sitem = item.Split(':');
    switch(sitem[0].Trim())
    {
        case "AccountID":
           account.Id = int.Parse(sitem[1]); break;
        case "Name":
           account.Name = int.Parse(sitem[1]); break;  
           ....
    }
}

Actualy i advice you that you sould keep it in Json/XML format. 实际上,我建议您将其保留为Json / XML格式。 But maybe BinaryFormatter helps your case here the some examples that you can use; 但是也许BinaryFormatter在这里可以使用一些示例来帮助您解决问题。

class Program {
    static void Main (string[] args) {
        // Create Object
        Account account = new Account {

            Id = 100,
            Name = "Berkay"
        };

        // You can use BinaryFormatter 
        IFormatter formatter = new BinaryFormatter ();
        Stream stream = new FileStream ("D:\\account.txt", FileMode.Create, FileAccess.Write);

        formatter.Serialize (stream, account);
        stream.Close ();

        stream = new FileStream ("D:\\account.txt", FileMode.Open, FileAccess.Read);
        Account readAccount = formatter.Deserialize(stream) as Account;

        System.Console.WriteLine(readAccount.Id);
        System.Console.WriteLine(readAccount.Name);

        Console.ReadLine();
    }

}

    // Serializable attribute
    [Serializable]
    public class Account {
    public int Id { get; set; }
    public string Name { get; set; }
}

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

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