Search code examples
c#linux.net-corefile-permissions

Create file with 777 permission with .net core console application


I'm trying to do a console application in .net core which will run on centOS.

This application will create a PDF file and write some things on it.

The application is able to create the file in the proper file but not write in it because of permission I guess since I have a UnauthorizedAccessException.

How can I create PDF file with 777 permission?

This is how I create and write into the file :

private static void DoProcessing(GenerationFile generationFile, IMyProgress progress)
{
    IConfiguration config = ConfJson.GetConfig();

    string tmpDirectoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fichiers");
    string fileName = string.Empty;
    Guid guid = Guid.NewGuid();
    List<string> lstOfNameFile = new List<string>();
    ConsoleSpinner spinner = new ConsoleSpinner();

    Console.WriteLine("Fichiers en cours de création (Peut être long en fonction de la taille des fichiers)");

    //Option pour le parallel For
    var optionsParallel = new ParallelOptions
    {
        MaxDegreeOfParallelism = Environment.ProcessorCount
    };

    //Creation de la liste des noms des fichiers
    for (int i = 0; i < generationFile.ArchiveESC; i++)
    {
        fileName = $"{tmpDirectoryPath}/{i + 1}_{guid}.pdf";
        lstOfNameFile.Add(fileName);
    }

    //Gestion du spinner dans la console
    ConsoleSpinner consoleSpinner = new ConsoleSpinner();
    object objectLock = new object();

    Console.Write($"\rCréation en cours : 0%    ");

    Parallel.ForEach(lstOfNameFile, item =>
    {
        using (Stream outStream = new FileStream(item, FileMode.Create, FileAccess.ReadWrite))
        using (Document outDoc = Document.Create(outStream, Conformance.PdfA2B, null))
        {
            //Taille de la page
            Size size = new Size
            {
                Height = 480,
                Width = 480
            };
            try
            {
                //Police de la page   
                Font font = Font.CreateFromSystem(outDoc, "Arial", "Bold", true);

                //Ajout des pages
                for (int j = 0; j < generationFile.SizeMaxFile; j++)
                {
                    Page page = Page.Create(outDoc, size);
                    AddText(outDoc, page, font, 14, fileName, j, generationFile.SizeMaxFile);
                    for (int k = 0; k < 292; k++)
                    {
                        AddImage(outDoc, page, 30, 30);
                    }
                    outDoc.Pages.Add(page);
                    lock (objectLock) { spinner.Turn(); }
                }
            } catch(Exception ex)
            {
                throw new AggregateException("Impossible de modifier le pdf pour atteindre la taille requise");
            }
            //Gestion progression pourcentage
            progress.Increment(1);
        }
    });

    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine("\rCréation des fichiers : 100%    ");

    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine($"Les fichiers ont été créé dans le dossier : {tmpDirectoryPath}", Console.ForegroundColor);
}

Solution

  • For anyone who stumble upon this, use the Mono.Posix nugget package. It has allo you need.