C# 3.0 用于捕获数据并将其保存到文本文件中的控制台应用程序…如何实现? 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.IO; 名称空间ABC { 班级计划 { 静态void Main(字符串[]参数) { 字符串用户输入; 列表a=新列表(); 做 { Console.WriteLine(“>>>名称

C# 3.0 用于捕获数据并将其保存到文本文件中的控制台应用程序…如何实现? 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.IO; 名称空间ABC { 班级计划 { 静态void Main(字符串[]参数) { 字符串用户输入; 列表a=新列表(); 做 { Console.WriteLine(“>>>名称,c#-3.0,C# 3.0,我没有运行您的代码,但是: a) 不要忘记关闭文件 b) 看起来您总是将这些“数据字段”添加到列表中(只需执行一次,每次添加新的数据集时将其清除)如果在同一文件上同时打开了FileStream和StreamWriter,则会导致错误 删除该行 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ABC { clas

我没有运行您的代码,但是:

a) 不要忘记关闭文件


b) 看起来您总是将这些“数据字段”添加到列表中(只需执行一次,每次添加新的数据集时将其清除)

如果在同一文件上同时打开了
FileStream
StreamWriter
,则会导致错误

删除该行

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

namespace ABC
{
    class Program
    {
        static void Main(string[] args)
        {

            string userInput;
            List<string> a = new List<string>();


            do
            {
                Console.WriteLine(">>> NAME <<<");
                Console.WriteLine("1 - Add");
                Console.WriteLine("0 - Exit");

                //get user's choice/input
                userInput = Console.ReadLine();

                //actions to take after user's choice/input
                switch (userInput)
                {
                    case "1":
                        //Add list to store info
                        Console.WriteLine("B");

                        //capture details
                        a.Add("Name: ");
                        a.Add("Surname: ");
                        a.Add("Address: ");
                        a.Add("Telephone: ");
                        a.Add("Cell: ");
                        a.Add("Email: ");
                        a.Add("Web: ");
                        a.Add("Date: ");

                        foreach (string i in a)
                        {
                            Console.Write(i);
                            Console.ReadLine();
                        }
                        FileStream fs = new FileStream("myfile.txt",FileMode.Create,FileAccess.ReadWrite);
                        StringBuilder sb = new StringBuilder();
                        foreach (string str in a)
                            {
                            StreamReader sr = new StreamReader();
                            {
                                sb.AppendLine(str.ToString());
                                sb.Append(sr.ReadToEnd());
                                sb.AppendLine();
                            }
                        }
                        StreamWriter sw = new StreamWriter(@"myfile.txt");
                        sw.Write(sb.ToString());

                        break;
                    case "0":
                        Console.WriteLine("BYE!!!");
                        break;
                    default:
                        Console.WriteLine("{0} is not a valid choice", userInput);
                        break;
                }
                //allow user to see results
                Console.Write("press 'Enter' to continue...");
                Console.ReadLine();
                Console.WriteLine();
            }
            // Keep going until the user wants to quit 
            while (userInput != "0");
        }
    }     
 }
重新放置

FileStream fs = new FileStream("myfile.txt",FileMode.Create,FileAccess.ReadWrite);

我们应该做到这一点

编辑:您的程序完全按照您的要求执行

下面是我认为您想要实现的功能:

案例“1”: //将列表添加到存储信息 控制台。写入线(“B”)


那么问题是什么?问题是什么?总是在提问时,不要忘记说明发生了什么,你认为应该发生什么,以及你已经采取了哪些步骤(失败了)为了解决这个问题,我想将用户输入保存到一个文本文件中。编译后,我想写入并保存到的文本文件被创建,但它是空的…希望它足够清晰。然后处理FileStream和StreamWriter。或者使用
using(StreamWriter sw=new StreamWriter(@“myfile.txt”){sw write(sb.ToString());}
…嗯,这是更好的选择。我尝试过你的方法,文本文件是创建的,但它是空的。我尝试过将所有数据附加到StringBuilder,并使用StreamWriter写入文件和StreamReader进行读取。创建了文件,但它不显示数据字段和用户输入,而是只显示数据字段。好的,那么Ob…如何我是否将用户输入保存到文本文件中?@user:text writer执行此操作…??我修改了上面的代码以反映必须保存多个条目,并在条目之间添加了分隔符…但实际上就是这样。Ob…u这样的美…thanx:-)
StreamWriter sw = new StreamWriter(@"myfile.txt");
sw.Write(sb.ToString());
using (TextWriter tw = File.CreateTex("myfile.txt")) {
    tw.Write(sb.ToString());
}
//capture details
a.Add("Name: ");
a.Add("Surname: ");
a.Add("Address: ");
a.Add("Telephone: ");
a.Add("Cell: ");
a.Add("Email: ");
a.Add("Web: ");
a.Add("Date: ");

StringBuilder sb = new StringBuilder();
foreach (string i in a)
{
    Console.Write(i);
    var entry = Console.ReadLine();
    sb.AppendFormat("{0}{1}\n", i, entry);
}

using (TextWriter tw = File.AppendTex("myfile.txt")) {
    tw.Write(sb.ToString()); 
    tw.WriteLine("-------------------------------------------------");
}

break;