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

Prabs 1 Reputation point
2021-07-16T12:30:58.16+00:00

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);
        }
    }
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
322 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,118 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Michael Taylor 47,966 Reputation points
    2021-07-16T13:51:47.267+00:00

    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
    0 comments No comments

  2. Carsten Riedel 11 Reputation points
    2021-07-18T19:33:49.42+00:00

    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.


  3. Craig Johnson 0 Reputation points
    2024-04-12T12:59:07.8833333+00:00
            foreach (string _Drive in _drives)
    
            {
    
            MessageBox.Show(_Drive.ToString());
    
                if(IsReadyDrive(_Drive) == true) // <--- start [1] - Logical Drive is Ready
    
                { 
    
                    MessageBox.Show("This drive is ready");
    
                    System.IO.DirectoryInfo LogicalDrive_Info = new DirectoryInfo(_Drive);
    
                    foreach (DirectoryInfo FolderName_Info in LogicalDrive_Info.GetDirectories())// Folder Name
    
                    {
    
                        MessageBox.Show("[ Drive ] : " + LogicalDrive_Info + " [ Folder ] : " + FolderName_Info.ToString());
    
                        string folder_Path = LogicalDrive_Info + FolderName_Info.ToString();
    
                        System.IO.DirectoryInfo SubFolder_Info = new DirectoryInfo(folder_Path);
    
                        try
    
                        {
    
                            foreach (DirectoryInfo SubFolder1_Info in SubFolder_Info.GetDirectories())
    
                            {
    
                                MessageBox.Show("[ Sub-Folder ] : " + SubFolder_Info + " [ Folder ] : " + SubFolder1_Info.ToString());
    
                            }
    
                        }
    
                        catch(Exception ex)
    
                        {
    
                            MessageBox.Show("File access denied! " + ex.ToString());
    
                        }
    
                    }
    
                }// <--- end [1] - Logical Drive is ready
    
                else// <--- start [2] - Logical Drive is Not ready
    
                { 
    
                  MessageBox.Show("This drive is Not ready"); 
    
                } // <--- end [2] - Logical Drive is Not ready
    
            }// end-of:  foreach (string _Drive in _drives)
    
    0 comments No comments

  4. Bruce (SqlWork.com) 55,686 Reputation points
    2024-04-12T16:00:59.0866667+00:00

    you should only be using /var/log if your app is a daemon. as suggested, su would be used to install and install would create the folder and give access to the proper users.

    if its an app run by the user, then you should be using $HOME/.local/state for the logs.

    0 comments No comments