C#如何读取用户输入并将其添加到文本文件中?

C#如何读取用户输入并将其添加到文本文件中?,c#,C#,我正在尝试制作一个C#应用程序,从.txt文件中读取和输出信息,然后允许用户在该文件末尾输入更多信息。我正在尝试以CSV格式编写文本文件,但在确定如何添加到文件底部时遇到了很多困难。似乎当我尝试时,它会覆盖文件的顶行。感谢您的帮助。这是到目前为止的代码,很抱歉出现了令人困惑的行-我一直在尝试许多不同的东西,我可以在网上找到,试图让它工作 class Program { static void Main(string[] args) { string UIName

我正在尝试制作一个C#应用程序,从.txt文件中读取和输出信息,然后允许用户在该文件末尾输入更多信息。我正在尝试以CSV格式编写文本文件,但在确定如何添加到文件底部时遇到了很多困难。似乎当我尝试时,它会覆盖文件的顶行。感谢您的帮助。这是到目前为止的代码,很抱歉出现了令人困惑的行-我一直在尝试许多不同的东西,我可以在网上找到,试图让它工作

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();
        }
    }
}
}
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

您的代码有两个问题

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

     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();
            }
        }
    }
    
    类程序
    {
    静态void Main(字符串[]参数)
    {
    字符串UIName=“”;
    字符串UIInvoice=“”;
    字符串UIDue=“”;
    字符串UIAmount=“”;
    使用(FileStream fs=newfilestream(@“C:/Accounts.txt”,FileMode.Open))
    使用(StreamReader sr=新StreamReader(fs))
    {
    字符串内容=sr.ReadToEnd();
    string[]lines=content.Split(新字符串[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);
    int lineCount=0;
    列表帐户=新列表();
    foreach(行中的字符串行)
    {
    string[]column=line.Split(',');
    如果(行数!=0)
    {
    账户=新账户();
    account.AccountName=列[0];
    account.InvoiceDate=第[1]列;
    account.DueDate=列[2];
    account.AmountDue=第[3]列;
    账户。添加(账户);
    }
    lineCount++;
    }
    Console.WriteLine(内容);
    }
    使用(FileStream fs=newfilestream(@“C:/Accounts.txt”,FileMode.Append))
    使用(StreamWriter sw=新StreamWriter(fs))
    {
    账户=新账户();
    account.AccountName=UIName;
    account.InvoiceDate=ui发票;
    account.DueDate=ui到期日;
    account.AmountDue=ui金额;
    //账户。添加(账户);
    WriteLine(“是否要输入其他数据?”);
    Console.WriteLine(“请输入帐户名:”);
    UIName=Console.ReadLine();
    Console.WriteLine(“请输入发票日期:”);
    UIInvoice=Console.ReadLine();
    Console.WriteLine(“请输入到期日期:”);
    UIDue=Console.ReadLine();
    Console.WriteLine(“请输入到期金额:”);
    UIAmount=Console.ReadLine();
    字符串全文=(UIName+”、“+UIInvoice+”、“+UIDue+”、“+UIAmount”);
    File.AppendAllText(“C:/Accounts.txt”,fullText+Environment.NewLine);//即使在将“\”s切换到“/”s之后,也无法以这种方式工作。它表示该文件正被另一个进程使用。
    Console.ReadLine();
    }
    }
    }
    
    您的代码存在多个问题

  • 您希望将数据附加到文件的末尾,但您正在使用
    OpenOrCreate
    模式打开
    FileStream
    <代码>打开或创建模式将文件指针放在文件的开头。因此,在这之后,无论您向文件写入什么,它都将覆盖文件的现有内容

  • 您正在打开
    FileStream
    StreamWriter
    ,但不使用它们将内容写入文件。您应该使用
    sw.WriteLine(全文)
    ,而不是
    File.AppendAllText

  • 此外,您在代码中的错误位置写入了文件内容

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

    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();
                }
            }
        }
    
    static void Main(字符串[]args)
    {
    字符串UIName=“”;
    字符串UIInvoice=“”;
    字符串UIDue=“”;
    字符串UIAmount=“”;
    var filePath=@“D:\Accounts.txt”;
    使用(FileStream fs=newfilestream(filePath,FileMode.OpenOrCreate))
    {
    使用(StreamReader sr=新StreamReader(fs))
    {
    字符串内容=sr.ReadToEnd();
    string[]lines=content.Split(新字符串[]{Environment.NewLine},
    StringSplitOptions.RemoveEmptyEntries);
    int lineCount=0;
    列表帐户=新列表();
    foreach(行中的字符串行)
    {
    string[]column=line.Split(',');
    如果(行数!=0)
    {
    账户=新账户();
    account.AccountName=列[0];
    account.InvoiceDate=第[1]列;
    account.DueDate=列[2];
    account.AmountDue=第[3]列;
    账户。添加(账户);
    }
    lineCount++;
    }
    Console.WriteLine(内容);
    }
    }
    使用(FileStream fs=newfilestream(filePath,FileMode.Append))
    {
    使用(StreamWriter sw=新StreamWriter(fs))
    {
    账户=新账户();
    account.AccountName=UIName;
    account.InvoiceDate=ui发票;
    account.DueDate=ui到期日;
    account.AmountDue=ui金额;
    //账户。添加(账户);
    WriteLine(“是否要输入其他数据?”);
    Console.WriteLine(“请输入帐户名:”);
    UIName=Console.ReadLine();
    控制台。WriteLine(“请输入发票
    
    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();
                }
            }
        }