C # Moving Files

1

I'm making an application, and at a certain point it does the following:

Checks whether a file to be created already exists in a directory (output), if it already exists, it sends it to the Errors folder, otherwise it sends it to the output folder.

It reaches a point where the program transfers the file to the bugs folder, saying that the file does not exist. For example, he just downloaded the file t(49).png and he reads the same file again! Funny it is only in some cases that he does it.

My code is this:

if (System.IO.File.Exists(entrada + "t (" + i + ").png")) {

    string[] datas1 = Spire.Barcode.BarcodeScanner.Scan(@"C:\QTRACK\Entrada\PNG\t (" + i + ").png");

    this.textBox1.Text = datas1[0];

    foreach(string code in datas1) {
        DirectoryInfo exit = new DirectoryInfo(@"C:/QTRACK/Erro/");
        FileInfo[] teste = exit.GetFiles("*.png");
        x = teste.Length + 1;

        for (c = x; c <= 1000000000; c++) {

            if (System.IO.File.Exists(saida + code + ".png")) {
                System.IO.File.Move(entrada.ToString() + "t (" + i + ").png", erro + "e" + c + ".png");

            } else {
                System.IO.File.Move(entrada.ToString() + "t(" + i + ").png", saida + code + ".png");
            }

        }
    }
} else {

}

It gives the error of FileNotFoundExecption , but it was he who changed the file and is trying to change again.

    
asked by anonymous 28.04.2015 / 18:40

1 answer

1

What happens is that because of your 1000000000 iterations loop, when the file does not exist at output System.IO.File.Exists(saida + code + ".png") == false it moves entrada.ToString() + "t (" + i + ").png" to saida + code + ".png"

However, the second time, System.IO.File.Exists(saida + code + ".png") == true because you already sent the file there, then it tries entrada.ToString() + "t (" + i + ").png" somewhere, but that file no longer exists in this location.

I do not understand why this loop is needed, but I think all your problems will be solved if you simply delete it.

    
29.04.2015 / 01:12