2016-02-20 28 views
-1

我的應用程序沒有管理員權限。日誌文件的放置位置在哪裏?該應用程序將被安裝到c:\programfiles\myapp通常放置日誌文件在哪裏?

+0

你使用什麼記錄器? – StepUp

+0

@StepUp Nothing ..只需保存未能處理的文件列表即可。 – techno

回答

1

如果你是在談論一個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來更改,但這也意味着每個用戶都有自己的日誌。如果您想爲所有用戶提供一個商店位置,則由您創建特定文件夾並應用所需的權限集。另外,如果您想要將這些文件顯示給用戶,則可以使用enum MyDocuments將它們放入其文檔文件夾中。

+0

'CommonApplicationData',需要管理員權限嗎? – techno

+0

有點看http://stackoverflow.com/questions/22107812/privileges-owner-issue-when-writing-in-c-programdata這意味着安裝程序的用戶可以在那裏創建和寫入,其他用戶也可以機器編號但是,這可以使用ApplicationData而不是CommonApplicationData進行更改,但這也意味着每個用戶都有自己的日誌。如果您想爲所有用戶提供一個商店位置,那麼您需要創建一個特定文件夾並應用所需的權限設置 – Steve

+0

我只需要爲當前用戶執行此操作。可以創建文件夾並寫入我的文檔以便用戶可以隨時閱讀。 – techno