The process cannot access the file because it is being used by another process

Asked

Viewed 1,544 times

1

The problem appears in the penultimate line of the code below, what can I do? NOTE: I have already tried to use "Using" in the Streamreader, but it has not solved.

        string CJ;
        string CJ1;
        string CJ2;
        StreamReader LJ = new StreamReader(Application.StartupPath + @"\Jo\" + cbJ.Text + ".ij");
        {
            CJ = LJ.ReadLine();
            LJ.Close();
        }
        StreamReader L1 = new StreamReader(Application.StartupPath + @"\Ja\" + cbJ1.Text + ".jad");
        {
            CJ1 = LJ1.ReadLine();
            LJ1.Close();
        }
        foreach (string newPath in Directory.GetFiles(CJ, "*", SearchOption.AllDirectories))
        {
            File.Copy(newPath, newPath.Replace(CJ, CJ1 + "/" + cbJ.Text), true);
        }
        StreamReader L2 = new StreamReader(Application.StartupPath + @"\Ja\" + cbJ2.Text + ".jad");
        {
            CJ2 = LJ2.ReadLine();
            LJ2.Close();
        }
        foreach (string newPath in Directory.GetFiles(CJ, "*", SearchOption.AllDirectories))
        {
            File.Copy(newPath, newPath.Replace(CJ2 + "/" + cbJ.Text, CJ), true);
        }
  • 2

    Put some clearer names on the variables that help locate the problem. You create L1, but close LJ1, create L2, but close LJ2, hard to track code like this.

  • In fact the code could be much simpler than that, which would avoid this error.

  • If you have a new question, ask a new question, do not change the answer in a way that completely changes the question. You had a problem and it was solved. If you have not solved everything, you can ask new questions.

  • Do the following Carlos, post this new code as a new question, since we passed this stage, and explain in the new how the directory is and how it is to stay after the function, that we try to help. Try to give more details, not only the code, and with examples of how you would like the directory before and after.

1 answer

3

If the code is only this one, it is a typo. See the difference in the variables LJ1 and LJ2:

    string CJ;
    string CJ1;
    string CJ2;
    StreamReader LJ = new StreamReader(Application.StartupPath + @"\Jo\" + cbJ.Text + ".ij");
    {
        CJ = LJ.ReadLine();
        LJ.Close();
    }
    StreamReader L1 = new StreamReader(Application.StartupPath + @"\Ja\" + cbJ1.Text + ".jad");
    {
        CJ1 = L1.ReadLine();
        L1.Close();
    }
    foreach (string newPath in Directory.GetFiles(CJ, "*", SearchOption.AllDirectories))
    {
        File.Copy(newPath, newPath.Replace(CJ, CJ1 + "/" + cbJ.Text), true);
    }
    StreamReader L2 = new StreamReader(Application.StartupPath + @"\Ja\" + cbJ2.Text + ".jad");
    {
        CJ2 = L2.ReadLine();
        L2.Close();
    }
    foreach (string newPath in Directory.GetFiles(CJ, "*", SearchOption.AllDirectories))
    {
        File.Copy(newPath, newPath.Replace(CJ2 + "/" + cbJ.Text, CJ), true);
    }

Now, if you explain what you want to do, better, because probably even correcting everything you need, the above code is very confused and probably with something you wouldn’t even need to achieve your goal.

You might be interested in this:

LJ = File.ReadLines(Application.StartupPath + @"\Jo\" + cbJ.Text + ".ij")).First();
L1 = File.ReadLines(Application.StartupPath + @"\Ja\" + cbJ1.Text + ".jad")).First();
L2 = File.ReadLines(Application.StartupPath + @"\Ja\" + cbJ2.Text + ".jad")).First();

About the Application.StartupPath, You need to see if this is what you really want. Escape the problem of the question, but I suggest studying how it works, to see if in a real situation is the best way to get the path you need.

  • Simply put: copy from LJ to L1 and then copy from L2 to LJ, automatically replacing files with the same name. About Application.Startuppath: Yes, I need that information at the root of the software. On the simplification of the code: thank you, this is very useful, I’m new in C# and I didn’t know how much could have made the code simpler.

  • It can copy from LJ pro L1, the problem appears in the copy of L2 pro LJ.

  • I did it the way you said (check the question again) but the problem persists in the second File.Copy, where I take the files from 3 and put pro 1, overwriting. I believe that some of the lines of my code is using the first directory, allowing copying from 1 pro 2, but preventing copying from 3 pro 1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.