在.NET中,我应该如何访问用于保存特定于当前计算机或用户的配置数据的文件夹(以避免硬编码路径)?
(相关:) Which is the best location to keep program configuration file in WINDOWS?
回答问题 ...顺便说一句,这是典型的WinXP输出
foreach (Environment.SpecialFolder f in
Enum.GetValues(typeof(Environment.SpecialFolder)))
Debug.WriteLine(string.Format("{0,16}: {1}",
f.ToString(), Environment.GetFolderPath(f)));
Desktop: C:\Documents and Settings\user\Desktop Programs: C:\Documents and Settings\user\Start Menu\Programs Personal: C:\Documents and Settings\user\My Documents Personal: C:\Documents and Settings\user\My Documents Favorites: C:\Documents and Settings\user\Favorites Startup: C:\Documents and Settings\user\Start Menu\Programs\Startup Recent: C:\Documents and Settings\user\Recent SendTo: C:\Documents and Settings\user\SendTo StartMenu: C:\Documents and Settings\user\Start Menu MyMusic: C:\Documents and Settings\user\My Documents\My Music DesktopDirectory: C:\Documents and Settings\user\Desktop MyComputer: Templates: C:\Documents and Settings\user\Templates ApplicationData: C:\Documents and Settings\user\Application Data LocalApplicationData: C:\Documents and Settings\user\Local Settings\Application Data InternetCache: C:\Documents and Settings\user\Local Settings\Temporary Internet Files Cookies: C:\Documents and Settings\user\Cookies History: C:\Documents and Settings\user\Local Settings\History CommonApplicationData: C:\Documents and Settings\All Users.WINDOWS\Application Data System: C:\WINDOWS\system32 ProgramFiles: C:\Program Files MyPictures: C:\Documents and Settings\user\My Documents\My Pictures CommonProgramFiles: C:\Program Files\Common Files
答案 0 :(得分:1)
除了必须位于应用程序文件夹中的App.config之外, 可以 将它们放在任何位置。不过,我建议您在应用程序文件夹下为所有必需的应用程序文件建立一个逻辑组织文件夹结构,并且所有配置文件的“ config ”文件夹肯定是好的开始。
请记住,如果应用程序文件夹中的配置文件 不 ,则必须在app.config中放置重定向行,以告知配置子系统在何处找到它......
<appSettings configSource="Config\AppSettings.config" />
答案 1 :(得分:1)
string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
答案 2 :(得分:1)
您可以使用 Environment.GetFolderPath()获取系统上特殊文件夹的路径,无论其位置如何,包括配置文件夹的标准位置和要保留的文件:
// Returns the user specific config folder
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
// Returns the computer specific config folder
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
然后,您可以将字符串附加到特定的应用程序文件夹,并将数据保存在那里。