2016-04-24 4 views
1

私は、Visual Studio内のC#コンソールアプリケーションを使用して大学生向けの登録プログラムを作成しています。私は外部ファイルの1つで問題が発生しました。どうやらそれは現在、別のプログラムで使用されており、修正方法を見つけることができません。今現在使用中の外部ファイルの問題

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つのストリームオブジェクトを作成しています。 2番目のストリームオブジェクトとまったく同じファイルにアクセスしている最初のファイルを閉じる、または処理していません。ステートメントを使用してStreamオブジェクトの呼び出しをラップする必要があるため、ファイル上の保持を自動的に破棄して解放します。

+0

、私は今あなたが何を意味するのかを理解します!ありがとうございます –

+0

申し訳ありません私は自分の携帯電話でコードを投稿するのが難しいです –

0

ファイルを書き込むときにも、 "using"ステートメントを強くお勧めします。

このように、作者は常にファイル/リソースを解放します。ミリテッロがちょうど掲示何から

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 =新規のStreamWriter( "CDriveDirs.txt")){foreachの (DirectoryInfo DIR cDirsで) {sw.WriteLine(dir.Name)。 } } 「使用中」ステートメントがファイルを使用している状態で保持していない間に中断した隠れた「デバッグ」プロセスがあるかもしれません。 – MilitelloVinx

関連する問題