Good day!
The problem is as follows:
It is necessary to copy the file with the transfer to the newly created folder.
First I create a folder and check it for existence and access rights - everything is in order with the rights - read and write.

public void CheckAndCreate(string path) { if (!(Directory.Exists(path))) { Directory.CreateDirectory(path); } else { Directory.CreateDirectory(path); } try { Directory.GetAccessControl(path); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } 

Next, copy the file:

 CheckAndCreate(@"\\172.16.0.7\Dogovor\" + id_dogovor); if (Path == "") { Path = @"\\172.16.0.7\Dogovor\" + id_dogovor + @"\" + id_dogovor + ".docx"; } while (!File.Exists(Path)) { File.Copy(directoryPath, Path, true); } 

As a result we get: The file is for some reason not always copied. Checked for all exceptions everything is in order. Although it is strictly stated that While there is no file - copy. I tried in different ways:

 try { FileInfo fn1 = new FileInfo(Path); while (true) { FileInfo fn = new FileInfo(directoryPath); fn.CopyTo(Path, true); if (fn1.Exists) break; } } catch (Exception ex) { if (IsCritical(ex)) { MessageBox.Show("Не удалось скопировать файл по причине: " + ex.Message); } } 

I also tried after creating the folder - create a text file for verification.

  System.IO.File.WriteAllText(@"\\172.16.0.7\Dogovor\" + new_dogovor + @"\TestFile.txt", "текст"); System.IO.File.AppendAllText(@"\\172.16.0.7\Dogovor\" + new_dogovor + @"\TestFile.txt", "текст"); StreamWriter file = new StreamWriter(@"\\172.16.0.7\Dogovor\" + new_dogovor + @"\TestFile.txt"); file.Write("4444444444444444444444444444444444444444444444444"); file.Flush(); file.Close(); 

But all the same, all the unsuccessful files persist not all are written. It works through time. Moreover, if you write the file in a few minutes after creating the folder, everything is copied successfully, everything was in order, anyway. About 50 people use this program. and periodically this problem occurs. I've been trying to solve for a long time - nothing comes of it. Please help!

  • If my memory serves me, in version 2 there will be an infinite loop, because you are not updating the information about the fn1 file. I can advise you to copy the file via file2.Write(file1.Open().ReadToEnd()) . Then surely it will be recorded. - Danatela
  • 3
    And what is this construction for you? if (! (Directory.Exists (path))) {Directory.CreateDirectory (path); } else {Directory.CreateDirectory (path); } - teanYCH
  • Well this is a little curve) - romkaisanin
  • file2.Write (file1.Open (). ReadToEnd ()). I did not understand a bit, but how do file1, file2 determine which type? - romkaisanin
  • while (!File.Exists(Path)) File.Copy Well, you're a sadist straight. If the file cannot be copied for some reason, your code will go on to infinity. - VladD

2 answers 2

@romkaisanin , miracles do not happen.

If a

  while (!File.Exists(Path)) File.Copy... 

passes (and you are absolutely sure that you get on this while ), and then it turns out that there is no file,

then you are looking for a mistake not there!

Somewhere after this place someone sometimes (!) Deletes the file (or you still do not always get to this while ).

    VladD is right that doing such a while-loop is too hard, because it is potentially infinite. But in fact it will not spin endlessly, because any exceptional situation will lead to its exit. And if this does not occur, the file will still be copied, and there will be a regular completion. I would suggest the following:

    1. Move the try-catch construct inside the loop body.
    2. After copying, add Sleep for at least 1 second to allow the file to be physically copied and not to jerk the system with new copy requests.
    3. Limit the number of attempts to something reasonable, for example 5 or 10 times.
    4. But for such a code, which seemed to me at least strange, you should be scolded:

       if (!(Directory.Exists(path))) { Directory.CreateDirectory(path); } else { Directory.CreateDirectory(path); } 

    Once, my radio leader asked: "Roma, what kind of juice will you have: peach-apple, peach-apple or peach-apple?" Besides, why create a directory when it already exists?