Я создаю программу регистрации для работы в колледже с помощью консольного приложения C# в visual studio, и я наткнулся на проблему с одним из моих внешних файлов. По-видимому, в настоящее время она используется другой программой, и я не могу найти, как ее исправить.Внешний файл, который в настоящее время используется.
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-файлов, мой единственный вариант? Спасибо заранее!
Из того, что Militello просто отвечал, теперь я понимаю, что ты имел в виду! спасибо –
Извините, что я нахожусь на моем телефоне, поэтому код проводки нелегкий –