我正在Visual Studio中使用C#控制檯應用程序構建大學工作的註冊程序,並且我偶然發現了一個外部文件的問題。顯然它正在被另一個程序使用,我找不到如何修復它。當前正在使用的外部文件問題
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文件的無限列表,這是我唯一的選擇嗎? 在此先感謝!
從什麼泰洛剛剛發佈,現在我明白你的意思!謝謝 –
對不起,我在我的手機上發佈代碼很難 –