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);
}
}