question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked cooldadtx commented

c# Environment.SpecialFolder.MyDocuments creating issue

I want to create a log folder in this location C:\Users\administrator\Documents i tried this code but below code not able to create directory in that location. program not throwing error but not able to create folder in Documents folder. i tried this code

 LogPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Log";
    
 if (!Directory.Exists(LogPath))
 {
     Directory.CreateDirectory(LogPath);
 }

where i made the mistake for which log folder not creating in document folder?
please share the right code. thanks

dotnet-csharp
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.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered cooldadtx commented

Your code looks correct. Are you sure you are looking at the correct directory? This code will create the folder under the Documents of the user running the code. Verify the full path set by LogPath is correct and then step through the code to confirm that Exists is returning true if it doesn't exist.

· 3
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 should also point out that you really shouldn't use string concat to put together paths. Use Path.Combine instead. This will handle the cases where one or both paths have slashes which will result in an invalid path, not the case here though.

0 Votes 0 ·

when i am trying in my pc then log folder is creating but when i test & run the same program in another pc then log folder is not creating in document folder. do i need to run the program as Run as administrator ?

0 Votes 0 ·

You're using the per-user environment folder so it'll work for any user. The only thing that will change is where the folder is created as it completely depends upon the user. Apps do this all the time as it is standard folder where apps store their data. Therefore there isn't going to be a permissions issue or anything like that for the current user. If there were every app would fail just about. If it were a permissions problem then an exception would occur as well.

I still believe you are probably looking in the wrong folder but you haven't confirmed that. Dump the value of LogPath and confirm that the path is correct for the user you are running the app as. If there is anything special about the app then let us know such as running as a scheduled task, a service, etc.

0 Votes 0 ·