Help with the problem. For educational purposes, I am writing a small C # program for working with PDF, using ItextSharp 7 library. In one of the classes I try to copy the file for rewriting using System.IO.File.Copy() , but in return I get an empty file. What could be wrong? Distinct answers in Google, I did not find

Here is the class code:

 string FilePath { get; set; } int Rotation { get; set; } // В этом методе проблема void RotateFile() { string dest = /* тут путь для сохранения */; if (FilePath != dest) // если файл назначения не совпадает с исходным, здесь все работает { if (File.Exists(FilePath)) { RotateThis(FilePath, dest); } else { MessageBox.Show("Файл не существует или был перемещен!"); } } else if (FilePath == dest) // А вот здесь проблема { try { FileInfo copier = new FileInfo(FilePath); if (!copier.Exists) { MessageBox.Show(this, "Файл не существует", "Ошибка!",MessageBoxButtons.OK, MessageBoxIcon.Error); } string temp = Path.Combine(copier.DirectoryName, DateTime.Now.ToString("ddMMHHmmss") + "temp.pdf"); if (Directory.Exists(copier.DirectoryName)) { try { // Вместо копирования, создает пустой файл File.Copy(copier.FullName, temp, true); } catch (IOException exception) { MessageBox.Show("Ошибка ввода-вывода при копировании во временный файл:" + exception.Message); } catch (Exception exc) { MessageBox.Show("Ошибка при копировании во временный файл" + exc.Message); } try { RotateThis(temp, FilePath); } catch (IOException exception) { MessageBox.Show("Ошибка ввода-вывода при повороте документа:" + exception.Message); } catch (Exception exc) { MessageBox.Show("Ошибка при повороте документа:" + exc.Message); } finally { File.Delete(temp); } } else { MessageBox.Show(this, "Файл не существует или был перемещен!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } void RotateThis(string src, string dest) { try { using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest))) { int numberOfPages = pdfDoc.GetNumberOfPages(); try { for (int p = 1; p <= numberOfPages; p++) { var page = pdfDoc.GetPage(p); var rotate = page.GetPdfObject().GetAsNumber(PdfName.Rotate); if (rotate == null) { page.SetRotation(Rotation); } else { page.SetRotation((rotate.IntValue() + Rotation) % 360); } } } catch (Exception exc) { MessageBox.Show("Ошибка: " + exc.Message); } finally { pdfDoc.Close(); MessageBox.Show(this, "Завершено !","Готово",MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } } catch (Exception e) { MessageBox.Show("Ошибка создания файла pdf: " + e.Message); } } 

Closed due to the fact that off-topic participants VladD , D-side , Abyx , aleksandr barakin , Bald Jun 19 '17 at 5:54 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - VladD, D-side, Abyx, aleksandr barakin, Bald
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Please give the program code. - Mikhail Rebrov
  • Show the minimum reproducible example . Without it, we can only guess at the tea leaves. Suddenly you suppressed exceptions and copy a non-existent file? - VladD
  • Added class code, no form. There are no exceptions, but the file is created empty. - Thomas Backer
  • If you have a problem in File.Copy(copier.FullName, temp, true); , specify what copier.FullName and temp equal copier.FullName and whether there are such files on the disk at this moment. - VladD
  • copier.FullName is C: \ Users \ Test \ Desktop \ src.pdf, temp is C: \ Users \ Test \ Desktop \ 1806083858temp.pdf. When calling the File.Copy method (copier.FullName, temp, true); The src.pdf file exists, to a temporary file ~ temp.pdf should be created by this method. It is created, only empty. - Thomas Backer

1 answer 1

I thank the respected VlaD for help in finding the real source of the problem.

The question is closed. The problem was not in System.IO.File.Copy ().

When calling a method

 Handyman handyman = new Handyman(); handyman.ViewSaveDialog(); string dest = handyman.SavePathfile; 

There was a creation of a new file, but since I chose an existing source file, it was overwritten as an empty file. System.IO.File.Copy (), respectively, absolutely correctly copied the empty file to an empty file.