2012-05-02 84 views
0

テキストファイルを1行上に移動して元のファイルに書き直していますが、何らかの理由でエラーが発生しました。それを理解しているようだ。プロセスがファイルを使用中であるためアクセスできません(エラー)

using (StreamReader reader = new StreamReader("file.txt")) 
{ 
    string line; 
    int Counter = 0; 

    while ((line = reader.ReadLine()) != null) 
    { 
     string filepath = "file.txt"; 
     int i = 5; 
     string[] lines = File.ReadAllLines(filepath); 

     if (lines.Length >= i) 
     { 
      string tmp = lines[i]; 
      lines[i] = lines[i-1]; 
      lines[i-1] = tmp; 
      File.WriteAllLines(filepath, lines); 
     } 
    } 
    Counter++; 
} 
+3

私はあなたがここでやっていることはちょっと狂っていると思います...この問題は、すでにStreamReaderで開いているファイルに書き込もうとしていることです。明示的に何をしようとしていますか?おそらく私たちはあなたの問題を解決するのを助けることができます。 –

+0

は、すべてのファイルの内容を配列またはリストに格納することができます。それらを動かしてから、全体を戻してください。whileループなし –

+0

ファイル内の各行を入れ替えますか? –

答えて

0

私はあなたが実際にそれぞれの行を交換したいと仮定し、(?):

string tmp = lines[i]; 
lines[i] = lines[i-1]; 
lines[i-1] = tmp; 

は、だからここに動作するはずのアプローチです:

String[] lines = System.IO.File.ReadAllLines(path); 
List<String> result = new List<String>(); 
for (int l = 0; l < lines.Length; l++) 
{ 
    String thisLine = lines[l]; 
    String nextLine = lines.Length > l+1 ? lines[l + 1] : null; 
    if (nextLine == null) 
    { 
     result.Add(thisLine); 
    } 
    else 
    { 
     result.Add(nextLine); 
     result.Add(thisLine); 
     l++; 
    } 
} 
System.IO.File.WriteAllLines(path, result); 

編集:これはあなたの要件であるとコメントしているので、前の行と1行だけを交換する少し修正したバージョンです:

String[] lines = System.IO.File.ReadAllLines(path); 
List<String> result = new List<String>(); 
int swapIndex = 5; 
if (swapIndex < lines.Length && swapIndex > 0) 
{ 
    for (int l = 0; l < lines.Length; l++) 
    { 
     String thisLine = lines[l]; 
     if (swapIndex == l + 1) // next line must be swapped with this 
     { 
      String nextLine = lines[l + 1]; 
      result.Add(nextLine); 
      result.Add(thisLine); 
      l++; 
     } 
     else 
     { 
      result.Add(thisLine); 
     } 
    } 
} 
System.IO.File.WriteAllLines(path, result); 
+0

ありがとう – user1285872

5

あなたはこのラインで読むためにファイルを開いている:

using (StreamReader reader = new StreamReader("file.txt")) 

この時点で、それが開いていて、使用されています。

あなたは、その後、後で持っている:

string[] lines = File.ReadAllLines(filepath); 

同じファイルから読み取るしようとしています。

達成しようとしていることは明確ではありませんが、これは機能しません。

私が見るところでは、readerはまったく必要ありません。あなたはそれを書くために、ファイルを開こうとしている

0

は、メソッドの内部で、それが既に開いているので、

0
、ファイルライターがそれを開こうとすると、ストリームリーダーがそれを開いて、それを開くためのStreamReaderを使用したが、カントすでにthatsのさ

同時にファイルを読み書きしない ファイルが小さい場合は、ロードして変更して書き戻します。 2.ファイルが巨大な場合は、出力用に別の一時ファイルを開き、 最初のファイルを削除/削除し、2番目のファイルの名前を変更します。代わりに持つの

0

using (StreamReader reader = new StreamReader("file.txt")) 
{ 
... 
string[] lines = File.ReadAllLines(filepath); 
} 

用途:

using (StreamReader reader = new StreamReader("file.txt")) 
{ 
string line; 
string[] lines = new string[20]; // 20 is the amount of lines 
int counter = 0; 
while((line=reader.ReadLine())!=null) 
{ 
    lines[counter] = line; 
    counter++; 
} 
} 

これは、ファイルからすべての行を読んで、 'ライン' に入れます。

コードの書き込み部分でも同じことができますが、この方法では1つのプロセスのみを使用してファイルから読み取ります。すべての行を読み込んで処理して閉じます。

希望の方はこちら!なぜなら、このコードスニペットのファイルに

関連する問題