user1285872
user1285872

Reputation: 83

The process cannot access the file because it is being used (error)

I'm trying to move line in a text file up by one then rewrite it back to the original file, but getting error for some reason, can't seem to figure it out.

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++;
}

Upvotes: 0

Views: 2292

Answers (5)

Tim Schmelter
Tim Schmelter

Reputation: 460288

I assume that you actually want to swap each line(?) in the file, because of this code snippet:

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

So here's an approach that should work:

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);

Edit: Here's the slightly modified version that swaps only one line with it's previous line, since you've commented that this is your requirement:

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);

Upvotes: 0

matthewr
matthewr

Reputation: 4749

Instead of having:

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

Use:

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++;
}
}

This will read all the lines from the file and put them into 'lines'.

You can do the same with the Writing part of the code, but this way reads from the file using only 1 process. It will read all the lines then dispose and close itself.

Hope this Helps!

Upvotes: 0

neohope
neohope

Reputation: 1832

do not read and write a file at the same time 1.if the file is small, just load it, change it, and write back. 2.if the file is huge, just open another temp file for output, the remove/delete the first file, and then rename the second file.

Upvotes: 0

RhysW
RhysW

Reputation: 453

Where you are trying to open the file to write it is inside a method thats already using a streamreader to open it, stream reader opens it, file writer tries to open it but cant because it is already open,

Upvotes: 0

Oded
Oded

Reputation: 499352

You are opening the file to read in this line:

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

At this point it is open and being used.

You then, later on have:

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

Trying to read from the same file.

It is not clear what you are trying to achieve, but this will not work.

From what I can see, you don't need the reader at all.

Upvotes: 5

Related Questions