2016-04-24 36 views
1

我正在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文件的無限列表,這是我唯一的選擇嗎? 在此先感謝!

回答

0

您正在創建2個流對象。您沒有關閉或處理正在訪問與第二個流對象完全相同的文件的第一個文件。您需要將您的Stream對象調用封裝在使用語句中,以便它們自動處理並釋放它們對文件的任何保留。

+0

從什麼泰洛剛剛發佈,現在我明白你的意思!謝謝 –

+0

對不起,我在我的手機上發佈代碼很難 –

0

我強烈建議您在編寫文件時使用「使用」語句。

  // 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

+0

我已經實現了使用代碼,但它仍然表示它正在使用中。難道你只能一次寫入一個txt文件?並且WriteLine的重複動作顯示爲「已在使用」 –

+0

我不這麼認爲。 這是MSDN示例中使用的代碼。正如你所看到的,它和你做的事情幾乎完全相同。 使用(StreamWriter sw = new StreamWriter(「CDriveDirs.txt」))foreach(DirectoryInfo dir in cDirs) { sw.WriteLine(dir.Name); } } 也許有一些隱藏的「調試」過程,當「使用」語句不存在時會中斷文件的使用。 – MilitelloVinx