-1

My app does not have admin rights.Where is the typical location where Log Files are placed? The app will be installed to c:\programfiles\myapp

techno
  • 6,100
  • 16
  • 86
  • 192

2 Answers2

1

If you are talking about a Windows desktop application then you should look at the enumeration Environment.SpecialFolder and use the Environment.GetFolderPath to store the logs in the CommonApplicationData

 // This should be the path to C:\ProgramData
 string commonDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

 // Now add your app name to the path above
 string myAppDataPath = Path.Combine(commonDataPath, "MyAppName");

 // Create the directory specific for your app (no error if it exists)
 Directory.CreateDirectory(myAppDataPath);

 // Prepare a log file with an embedded time stamp for today
 string logFile = "MyAppData" + DateTime.Today.Year + 
                                DataTime.Today.Month.ToString("D2") +
                                DataTime.Today.Day.ToString("D2") + ".LOG";

 // And finally here the name for your logging activities
 string fullLogFile = Path.Combine(myAppDataPath, logFile);

Be aware that according to this question only the user that creates this files could write there. Other users on the same machine no. However this could be changed using ApplicationData instead of CommonApplicationData but this also means that each user has its own log. If you want one store location for all of your users then it is up to you to create a specific folder and apply the permissions set required. Also if you want to show these files to your users then you could place them in its document folder using the enum MyDocuments.

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Does `CommonApplicationData` ,require admin rights? – techno Feb 20 '16 at 11:34
  • Somewhat see http://stackoverflow.com/questions/22107812/privileges-owner-issue-when-writing-in-c-programdata This means that the user that install the program could create and write there, other users on the same machine no. However this could be changed using ApplicationData instead of CommonApplicationData but this also means that each user has its own log. If you want one store location for all of your users then it is up to you to create a specific folder and apply the permissions set required – Steve Feb 20 '16 at 11:39
  • I just need to do this for the current user.Can i create a folder and write to my documents so that the user can read it whenever he/she wants easily. – techno Feb 20 '16 at 11:43
  • Yes, if the requirement is to facilitate the reading of these files to your user, then the same Environment.SpecialFolder enum has an entry for MyDocuments – Steve Feb 20 '16 at 11:44
  • okay... i hope i dont need any special rights for that. – techno Feb 20 '16 at 12:28
-1

Try with Environment.SpecialFolder.ApplicationData

Marko Juvančič
  • 5,792
  • 1
  • 25
  • 41