C#如何读取和添加用户输入到文本文件?

时间:2018-01-04 06:18:40

标签: c#

我正在尝试创建一个C#应用程序,它从.txt文件中读取和输出信息,然后允许用户在该文件的末尾输入更多信息。我试图以CSV格式编写文本文件,并且我在确定如何添加到文件的BOTTOM时遇到了很多麻烦。看来,当我尝试时,它会覆盖文件的顶行。任何帮助表示赞赏。到目前为止,这是代码,对于任何令人困惑的线路感到抱歉 - 我一直在尝试许多不同的东西,我可以在网上找到它,试图让它工作。

class Program
{
    static void Main(string[] args)
    {
        string UIName = "";
        string UIInvoice = "";
        string UIDue = "";
        string UIAmount = "";

        using (FileStream fs = new FileStream(@"C:\Accounts.txt", FileMode.Open))
        using (StreamReader sr = new StreamReader(fs))
        {
            string content = sr.ReadToEnd();
            string[] lines = content.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            int lineCount = 0;
            List<Account> accounts = new List<Account>();
            foreach (string line in lines)
            {
                string[] column = line.Split(',');
                if (lineCount != 0)
                {
                    Account account = new Account();
                    account.AccountName = column[0];
                    account.InvoiceDate = column[1];
                    account.DueDate = column[2];
                    account.AmountDue = column[3];
                    accounts.Add(account);
                }
                lineCount++;
            }
            Console.WriteLine(content);
        }
        using (FileStream fs = new FileStream(@"C:\Accounts.txt", FileMode.OpenOrCreate))
        using (StreamWriter sw = new StreamWriter(fs))
        {
            Account account = new Account();
            account.AccountName = UIName;
            account.InvoiceDate = UIInvoice;
            account.DueDate = UIDue;
            account.AmountDue = UIAmount;
            //accounts.Add(account);
            string fullText = (UIName + "," + UIInvoice + "," + UIDue + "," + UIAmount);
                    Console.WriteLine("Would you like to enter additional data?");


            Console.WriteLine("Please enter the Account Name: ");
            UIName = Console.ReadLine();
            Console.WriteLine("Please enter the Invoice Date: ");
            UIInvoice = Console.ReadLine();
            Console.WriteLine("Please enter the Due Date: ");
            UIDue = Console.ReadLine();
            Console.WriteLine("Please enter the AmountDue: ");
            UIAmount = Console.ReadLine();

            File.AppendAllText("C:/Accounts.txt", fullText + Environment.NewLine);//can't get this way working, even after switching "\"s to "/"s. It says that the file is being used by another process.

            Console.ReadLine();
        }
    }
}
}

单独的课程:

public class Account
{
    public string AccountName { get; set; }
    public string InvoiceDate { get; set; }
    public string DueDate { get; set; }
    public string AmountDue { get; set; }

    public static string GetAccountCSV(Account account)
    {
        string returnValue = account.AccountName + "," + account.InvoiceDate + "," + account.DueDate + "," + account.AmountDue;
        return returnValue;
    }
}

.txt文件说;

Account Name,Invoice Date,Due Date,Amount Due
Jane Doe,1/12/2017,2/12/2017,2000.00
Gonuts Inc,12/31/2017,2/28/2017,1566.50

2 个答案:

答案 0 :(得分:0)

您的代码存在两个问题

  1. 您必须以追加模式@ref chetan comment
  2. 打开文件
  3. 您正在从用户输入读取之前创建全文字符串,因为它将空字符串写入文本文件。
  4. 请在下面找到工作程序类文件

     class Program
    {
        static void Main(string[] args)
        {
            string UIName = "";
            string UIInvoice = "";
            string UIDue = "";
            string UIAmount = "";
    
            using (FileStream fs = new FileStream(@"C:/Accounts.txt", FileMode.Open))
            using (StreamReader sr = new StreamReader(fs))
            {
                string content = sr.ReadToEnd();
                string[] lines = content.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    
                int lineCount = 0;
                List<Account> accounts = new List<Account>();
                foreach (string line in lines)
                {
                    string[] column = line.Split(',');
                    if (lineCount != 0)
                    {
                        Account account = new Account();
                        account.AccountName = column[0];
                        account.InvoiceDate = column[1];
                        account.DueDate = column[2];
                        account.AmountDue = column[3];
                        accounts.Add(account);
                    }
                    lineCount++;
                }
                Console.WriteLine(content);
            }
            using (FileStream fs = new FileStream(@"C:/Accounts.txt", FileMode.Append))
            using (StreamWriter sw = new StreamWriter(fs))
            {
                Account account = new Account();
                account.AccountName = UIName;
                account.InvoiceDate = UIInvoice;
                account.DueDate = UIDue;
                account.AmountDue = UIAmount;
                //accounts.Add(account);
                Console.WriteLine("Would you like to enter additional data?");
    
    
                Console.WriteLine("Please enter the Account Name: ");
                UIName = Console.ReadLine();
                Console.WriteLine("Please enter the Invoice Date: ");
                UIInvoice = Console.ReadLine();
                Console.WriteLine("Please enter the Due Date: ");
                UIDue = Console.ReadLine();
                Console.WriteLine("Please enter the AmountDue: ");
                UIAmount = Console.ReadLine();
                string fullText = (UIName + "," + UIInvoice + "," + UIDue + "," + UIAmount);
                File.AppendAllText("C:/Accounts.txt", fullText + Environment.NewLine);//can't get this way working, even after switching "\"s to "/"s. It says that the file is being used by another process.
                Console.ReadLine();
            }
        }
    }
    

答案 1 :(得分:0)

您的代码存在多个问题。

  1. 您希望将数据附加到文件的末尾,但是您使用FileStream模式打开OpenOrCreateOpenOrCreate模式将文件指针放在文件的开头。因此,无论您写入文件,它都会覆盖文件的现有内容。

  2. 您正在打开FileStreamStreamWriter,但不要使用它们将内容写入文件。而不是File.AppendAllText您应该使用sw.WriteLine(fullText)

  3. 此外,您正在将代码中的错误位置写入文件内容。

    以下是删除了上述所有问题的代码。

    static void Main(string[] args)
        {
            string UIName = "";
            string UIInvoice = "";
            string UIDue = "";
            string UIAmount = "";
            var filePath = @"D:\Accounts.txt";
            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    string content = sr.ReadToEnd();
                    string[] lines = content.Split(new string[] {Environment.NewLine},
                        StringSplitOptions.RemoveEmptyEntries);
    
                    int lineCount = 0;
                    List<Account> accounts = new List<Account>();
                    foreach (string line in lines)
                    {
                        string[] column = line.Split(',');
                        if (lineCount != 0)
                        {
                            Account account = new Account();
                            account.AccountName = column[0];
                            account.InvoiceDate = column[1];
                            account.DueDate = column[2];
                            account.AmountDue = column[3];
                            accounts.Add(account);
                        }
                        lineCount++;
                    }
                    Console.WriteLine(content);
                }
            }
    
            using (FileStream fs = new FileStream(filePath, FileMode.Append))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    Account account = new Account();
                    account.AccountName = UIName;
                    account.InvoiceDate = UIInvoice;
                    account.DueDate = UIDue;
                    account.AmountDue = UIAmount;
                    //accounts.Add(account);
    
                    Console.WriteLine("Would you like to enter additional data?");
                    Console.WriteLine("Please enter the Account Name: ");
                    UIName = Console.ReadLine();
                    Console.WriteLine("Please enter the Invoice Date: ");
                    UIInvoice = Console.ReadLine();
                    Console.WriteLine("Please enter the Due Date: ");
                    UIDue = Console.ReadLine();
                    Console.WriteLine("Please enter the AmountDue: ");
                    UIAmount = Console.ReadLine();
    
                    string fullText = (UIName + "," + UIInvoice + "," + UIDue + "," + UIAmount);
    
                    sw.WriteLine(fullText);
    
                    Console.ReadLine();
                }
            }
        }