我的StreamWriter没有给出错误,但是没有写入.txt文件?

时间:2014-10-16 06:34:18

标签: c# .net visual-studio streamwriter

感谢您的所有建议!

我真的很困惑为什么这仍然不起作用,'Customers.txt'只包含在解决方案中,并且使用StreamReader打开它很好,这是我的完整代码:/

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

namespace CustomerDetails
{
class Program
{
    class userDetails
    {
        public static List<string> firstName;
        public static List<string> lastName;
        public static List<string> telNumber;
        public static List<string> birthDate;
        public static List<string> postCode;
        public static string userDecision;
    }

    static void Main(string[] args)
    {
        int x = 1;
        userDetails.firstName = new List<string>();
        userDetails.lastName = new List<string>();
        userDetails.birthDate = new List<string>();
        userDetails.telNumber = new List<string>();
        userDetails.postCode = new List<string>();

        while (x == 1)
        {
            Console.WriteLine("------------------------------");
            Console.WriteLine("       CUSTOMER DATABASE      ");
            Console.WriteLine("------------------------------");
            Console.WriteLine("1.) Add Customer(s)");
            Console.WriteLine("2.) List Customers");
            Console.WriteLine("3.) Exit");
            Console.WriteLine("------------------------------");
            userDetails.userDecision = Console.ReadLine().Trim().ToUpper().Replace(" ", "");
            if (userDetails.userDecision == "1" ||
                userDetails.userDecision == "2" ||
                userDetails.userDecision == "3")
                break;
            else
                Console.Clear();
        }

        if (userDetails.userDecision == "3") { Environment.Exit(0); }

        Console.Clear();
        Console.WriteLine("------------------------------");
        Console.WriteLine("       CUSTOMER DATABASE      ");
        Console.WriteLine("------------------------------");

        if (userDetails.userDecision == "1")
        {
            int y = 0;

            while (y > -1)
            {
                string input;
                Console.Clear();
                Console.WriteLine("------------------------------");
                Console.WriteLine("         NEW CUSTOMER         ");
                Console.WriteLine("------------------------------");
                Console.Write("First Name: ");
                userDetails.firstName.Add(Console.ReadLine());
                Console.Write(" Last Name: ");
                userDetails.lastName.Add(Console.ReadLine());
                Console.Write("       DOB: ");
                userDetails.birthDate.Add(Console.ReadLine());
                Console.Write("Tel Number: ");
                userDetails.telNumber.Add(Console.ReadLine());
                Console.Write(" Post Code: ");
                userDetails.postCode.Add(Console.ReadLine());
                Console.WriteLine("------------------------------");
                int e = 0;
                while (e == 0)
                {
                    Console.Write("Add Another? Y/N:");
                    userDetails.userDecision = Console.ReadLine().ToUpper();
                    if (userDetails.userDecision == "Y" || userDetails.userDecision == "N")
                        e = 1;
                }
                if (userDetails.userDecision == "N")
                {
                    break;
                }
            }

            StreamWriter fileWriter = new StreamWriter(File.Open("Customers.txt", FileMode.Append));

            int v = 0;

            foreach (string element in userDetails.firstName)
            {
                fileWriter.WriteLine("/-----------\\");
                fileWriter.WriteLine(userDetails.firstName[v]);
                fileWriter.WriteLine(userDetails.lastName[v]);
                fileWriter.WriteLine(userDetails.postCode[v]);
                fileWriter.WriteLine(userDetails.birthDate[v]);
                fileWriter.WriteLine(userDetails.telNumber[v]);
                fileWriter.WriteLine("\\-----------/");
                v++;
                Console.WriteLine("DOING.");
            }
            fileWriter.Dispose();
            fileWriter.Close();
            Console.WriteLine("DONE.");
            Console.ReadLine();
        }
        // LIST CUSTOMER DETAILS

        //else if (userDetails.userDecision == "2")
        //{
        //    StreamReader fileReader = new StreamReader("Customers.txt");
        //    string currentLine = "";

        //    while (currentLine != null)
        //    {
        //        currentLine = fileReader.ReadLine();

        //        if (currentLine != null) {
        //            if (currentLine != "/-----------\\") {
        //                if(currentLine == "\\-----------/")
        //                    Console.WriteLine();
        //                else
        //                Console.WriteLine(currentLine); } }
        //    }
        //    fileReader.Close();

        //}
        //Console.ReadLine();
    }
}
}

2 个答案:

答案 0 :(得分:2)

您在循环运行时打开文件的次数。

您需要打开文件,然后输入您的循环代码,然后确保它关闭。

StreamWriter file= new StreamWriter(File.Open("fileName.txt", FileMode.CreateNew));

foreach (string element in userDetails.firstName)
{
   file.WriteLine("testing 1 10 11");

}

file.Close();
file.Dispose();

似乎using会为您关闭该文件,但我仍然更喜欢file.Close()file.Dispose(),直到我在using上阅读更多内容。

答案 1 :(得分:0)

更改

using (StreamWriter fileWriter = new StreamWriter("Customers.txt"))

行到

using (StreamWriter fileWriter = new StreamWriter(@"C:\Customers.txt"))

如果您在c:\上看到一个文件,则问题是文件正在写入但不是您期望的位置。可执行文件旁边是这样的代码的公共位置,但不是唯一的。