question

Prabs-7622 avatar image
0 Votes"
Prabs-7622 asked Prabs-7622 commented

Directory.CreateDirectory error on linux using C#.net core - Error: access path is denied

Hello All,
I cannot create directory on linux ,it is giving error. The error is "access to the path is denied"
I am using following code in c#.net core

if (!Directory.Exists(logPath))
{
Directory.CreateDirectory(logPath);
}

Please help me on this

dotnet-csharpdotnet-runtime-frameworkdotnet-runtime-coredotnet-console
· 7
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.

What's the value of "logPath"? You cannot create a directory at any path.

0 Votes 0 ·

Hi,I have given the following path,
Home/ProjectName/Log/

0 Votes 0 ·

What's your intention to use a relative path like Home/ProjectName/Log/? The working directory you execute the code determines the absolute path of Home/ProjectName/Log/ and that result can still be a path you don't have access to. To test out better, set logPath to an absolute path, which immediately tells you what's up.

0 Votes 0 ·

Yes,I used other path also,it is giving same error."Access to the path is denied."
other path : Home/Documents/ProjectName/Log/

Please help me on this

0 Votes 0 ·

I think he means "/Home/ProjectName/Log/" instead of "Home/ProjectName/Log/", provided you've created "/Home/ProjectName/Log/" and granted write access to the user running that application.

Although I think the more common location would be "/var/log/ProjectName/" or "$HOME/.ProjectName/log", I should add. (Putting "." in front of file/directory name will make it hidden by default)

0 Votes 0 ·
Show more comments
Prabs-7622 avatar image
0 Votes"
Prabs-7622 answered APoblacion commented

86213-linu.jpg



Please find the path location snapshot in ubuntu linux.
I created folder ("Home/Test/Log/") manually..but again !Directory.Exists(logPath) returning false and it will go if block,

if (!Directory.Exists(logPath))
{
Directory.CreateDirectory(logPath);
}


linu.jpg (50.0 KiB)
· 1
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.

I created folder ("Home/Test/Log/") manually...

The problem is that your folder name is missing the initial "/". In order to reliably create the folder, you need to specify an absolute path. This means that it has to start with a "/" and then list all the intermediate folders starting from the root of the filesystem. Otherwise it could be interpreted as being "anywhere" (actually it is taken from the current directory, which could be anything depending on how you start the program. No, the current directory is not your home directory, and it is also not the directory where you place the executable of your program).

In summary, something like this (but adapted to your filesystem): "/users/yourName/Home/Documents/ProjectName/Log". Do not forget the "/" at the beginning, and do not add a "/" at the end.

0 Votes 0 ·
lextm avatar image
0 Votes"
lextm answered lextm edited

Like I commented above, you specified an invalid path of Home/Test/Log.

Please study articles such as this to get started.

The actual path value of the folder you created is Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Test", "Log").

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.

Prabs-7622 avatar image
0 Votes"
Prabs-7622 answered Prabs-7622 commented

Hello all,
In linux system do we need to give permission for folders? like below,

sudo chmod -R a+rwx /home

If i give this command then log file creation is happening.
Please help me on this..

· 2
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.

/home is a different file path (root directory of all users' home directories) than what you showed earlier. You shouldn't change its permissions, and instead should follow my answer above.

0 Votes 0 ·

Hi,
This Environment.GetFolderPath(Environment.SpecialFolder.Personal) is giving Home directory in linux.

Now the log file is created in /Home/Test/Log/log.txt

Home and home directory both are different in Linux?

if i go Home or home in linux,both are pointing to same location

0 Votes 0 ·