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!
fn1file. I can advise you to copy the file viafile2.Write(file1.Open().ReadToEnd()). Then surely it will be recorded. - Danatelawhile (!File.Exists(Path)) File.CopyWell, you're a sadist straight. If the file cannot be copied for some reason, your code will go on to infinity. - VladD