【发布时间】:2016-06-02 01:05:25
【问题描述】:
我的应用没有管理员权限。日志文件放置的典型位置在哪里?该应用将安装到c:\programfiles\myapp
【问题讨论】:
-
你用什么记录器?
-
@StepUp Nothing.. 只需保存处理失败的文件列表即可。
我的应用没有管理员权限。日志文件放置的典型位置在哪里?该应用将安装到c:\programfiles\myapp
【问题讨论】:
如果您谈论的是 Windows 桌面应用程序,那么您应该查看枚举 Environment.SpecialFolder 并使用 Environment.GetFolderPath 将日志存储在 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);
请注意,根据this question,只有创建此文件的用户才能在此处写入。同一台机器上的其他用户没有。然而,这可以使用ApplicationData 而不是CommonApplicationData 来改变,但这也意味着每个用户都有自己的日志。如果您希望所有用户都拥有一个存储位置,那么您可以创建一个特定文件夹并应用所需的权限集。此外,如果您想向用户显示这些文件,则可以使用枚举 MyDocuments 将它们放在其文档文件夹中。
【讨论】:
CommonApplicationData 是否需要管理员权限?