The process can not access the file because it is being used by another process [closed]

1

The problem appears in the penultimate line of the code below, what can I do? Note: I've already tried using StreamReader, but it did not work.

        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);
        }
    
asked by anonymous 28.11.2016 / 03:33

1 answer

3

If the code is just the same, 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, the better, because probably even correcting everything you need, the above code is very confusing and probably with something you would not even need to achieve your goal.

Maybe you're interested in this here:

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 Application.StartupPath , you need to see if that's what you want. Get away from the problem of the question, but I suggest you study straight how it works, to see if a real situation is the best way to get the path you need.

    
28.11.2016 / 03:43