question

Prabs-7622 avatar image
0 Votes"
Prabs-7622 asked CarstenRiedel answered

How to solve Access to the path is denied Error in C#.NET Core application

Hi Team,

I have an C#.Net core 3.1 console application which is running on RedHat Linux.
I have to create a file in the following path in Linux OS using C#.Net Core.

"var/log/TEST_LOG/"

When i run at root user, the file is created succcessfully.
But when i run at normal user, the file is not created and giving following exception in Linux machine

Error:
Access to the path "var/log/TEST_LOG/" is denied.

Please help me on this issue.
My requirement is application has to run at normal user not root user.

Please find the following code,

static void CreateLogs()
{

         string linux_path = "";            
         linux_path = @"var/log/TEST_LOG/";                   

         try
         {
             string directoryPath = Path.Combine("/", linux_path);

             if (!Directory.Exists(directoryPath))
             {
                 Directory.CreateDirectory(directoryPath);
                 Console.WriteLine("Directory path created " + directoryPath);
             }

             string filePath = directoryPath + "EventLog.txt";

             Console.WriteLine("file path " + filePath);

             FileStream m_LogFile = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);

             using (StreamWriter m_LogFileWriter = new StreamWriter(m_LogFile, Encoding.UTF8))
             {
                 m_LogFileWriter.WriteLine("This file contains C#.");
                 m_LogFileWriter.Flush();
             }              

         }
         catch (Exception exp)
         {
             Console.WriteLine(exp.Message + " " + exp.StackTrace);
         }
     }




dotnet-csharpdotnet-runtimedotnet-cli
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

By default only the root user can write to var. This is by design and your app cannot change that.

Solutions.
1) As part of your app installation, while root, create the var/log folder and give whatever user(s) need to run your app write permissions to the directory
2) Pick a different directory that your user would have write permissions to
3) (Not a good idea) Have the root user give write access to the var folder to the user running your app

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

CarstenRiedel avatar image
0 Votes"
CarstenRiedel answered CarstenRiedel edited

Hi this guide should help.

https://www.linux.com/topic/desktop/how-manage-users-groups-linux/#:~:text=How%20to%20Manage%20Users%20with%20Groups%20in%20Linux,group%20access%20to%20that%20directory.%20Weitere%20Artikel...%20

The guide describes , add user to group , assign group to directory. (Read/Edit)

I would personally choose and other directory like /var/lib/appname/logs.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.