外部文件当前正在使用中

时间:2016-04-24 03:29:32

标签: c# visual-studio streamwriter

我正在使用visual studio中的c#console应用程序为大学工作构建注册程序,而且我偶然发现了我的一个外部文件的问题。显然它目前正由另一个程序使用,我无法找到解决方法。

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

namespace Class_Register_V1._0
{
    class Program
    {
        static void Main(string[] args)
        {
            int monday, tuesday, wednesday, thursday, friday, classSize, weekCount, periodsPerWeek, counter, count, Value, input2, Outcome;
            string answer, input, test;
            Value = 0;
            Outcome = 0;
            StreamReader Size = new StreamReader(@"C:\Users\Dannys\Desktop\RegisterArraySize.txt");
            count = Convert.ToInt32(Size.ReadLine());
            Size.Close();
            int newV = count - 1;
            int[] myListOfUsername = new int[count];
            StreamReader LogIn = new StreamReader(@"C:\Users\Dannys\Desktop\RegisterLogIn.txt");
            for (int NewA = 0; NewA < count; NewA++)
            {
                myListOfUsername[NewA] = Convert.ToInt32(LogIn.ReadLine());
            }
                Console.WriteLine("Welcome to CodeTown's ClassLogIn.");
            Console.WriteLine("1. login\n2. Register\n3. Exit");
            input = Console.ReadLine();
            input2 = Convert.ToInt32(input);
            Console.Clear();
            switch (input)
            {
                case "1":
                    do
                    {
                        Console.WriteLine("Please enter your login");
                        Value = Convert.ToInt32(Console.ReadLine());

                        for (int NewB = 0; NewB < count; NewB++)
                        {
                            if (Value == myListOfUsername[NewB])
                            {
                                Outcome = 1;
                                Console.Clear();
                            }
                        }
                        if (Outcome == 0)
                        {
                            Console.WriteLine("Login not recognised");
                            Console.ReadKey();
                            Console.Clear();
                        }
                    } while (Outcome == 0);
                    break;

                case "2":
                    {
                        do
                        {
                            Console.WriteLine("Create 6 number Login ID\ni.e. 936548");
                            test = Console.ReadLine();
                            if (test.Length > 6)
                            {
                                Console.Clear();
                                Console.WriteLine("The login you used was too long!");
                                Console.ReadKey();
                            }
                            if (test.Length < 6)
                            {
                                Console.Clear();
                                Console.WriteLine("The login you used was too short!");
                                Console.ReadKey();
                            }
                            Console.Clear();
                        } while (test.Length != 6);

                        count++;
                        newV = count - 1;
                        int[] ListOfUsername = new int[count];
                        StreamReader LogInCreate = new StreamReader(@"C:\Users\Dannys\Desktop\RegisterLogIn.txt");
                        for (int NewC = 0; NewC < count; NewC++)
                        {
                            ListOfUsername[NewC] = Convert.ToInt32(LogIn.ReadLine());
                        }
                        ListOfUsername[newV] = Convert.ToInt32(test);
                        Console.WriteLine("Login will be registered once ClassLogIn has been restarted.\nWelcome abord!");
                        StreamWriter array = new StreamWriter(@"C:\Users\Dannys\Desktop\RegisterArraySize.txt");
                        array.WriteLine(count);
                        array.Close();
                        StreamWriter LogInWrite = new StreamWriter(@"C:\Users\Dannys\Desktop\RegisterLogIn.txt");
                        for (int NewD = 0; NewD < count; NewD++)
                        {
                            LogInWrite.WriteLine(ListOfUsername[NewD]);
                        }
                        LogInWrite.Close();
                        Console.ReadKey();
                        Console.Clear();
                    }
                    break;

                case "3":
                    {

                    }
                    break;
            }
        }
    }
}

现在,正在创建错误的代码段

StreamWriter LogInWrite = new StreamWriter(@"C:\Users\Dannys\Desktop\RegisterLogIn.txt");
for (int NewD = 0; NewD < count; NewD++)
{
    LogInWrite.WriteLine(ListOfUsername[NewD]);
}
LogInWrite.Close();

我唯一想到的可能导致这种情况的原因是你每个文件只能使用一次WriteLine,因为它实际上并没有像在控制台中那样写一行,所以我需要创建每个用户名的新文件。如果这实际上是问题,使用循环创建一个可能无穷无尽的6位txt文件列表我唯一的选择? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

您正在创建2个流对象。您没有关闭或处置第一个访问与第二个流对象完全相同的文件。您需要在using语句中包装Stream对象调用,以便它们自动Dispose并释放它们对文件的任何保留。

答案 1 :(得分:0)

我强烈建议您在编写文件时使用“using”语句。

            // Write each directory name to a file.
            using (StreamWriter LogInWrite = new StreamWriter(@"C:\Users\Dannys\Desktop\RegisterLogIn.txt"))
            {
               for (int NewD = 0; NewD < count; NewD++)
                  LogInWrite.WriteLine(ListOfUsername[NewD]);
            }

通过这种方式,您的编写者将始终释放文件/资源​​。

https://msdn.microsoft.com/en-us/library/system.io.streamwriter(v=vs.110).aspx https://msdn.microsoft.com/en-ca/library/yh598w02.aspx