检测应用程序数据的位置\ LocalLow [英] Detect the location of AppData\LocalLow
问题描述
我试图找到该的AppData \ LocalLow
文件夹的路径。
I'm trying to locate the path for the AppData\LocalLow
folder.
我发现它使用一个例子:
I have found an example which uses:
string folder = "c:\users\" + Environment.UserName + @"\appdata\LocalLow";
这对于一个依赖于 C:
和用户
这似乎有点脆弱
我试图用
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
但是这给了我应用程序数据\本地
,我需要LocalLow由于应用程序在其下运行的安全约束。这回我的服务的用户(至少连接到进程时)空。
but this gives me AppData\Local
, and I need LocalLow due to the security constraints the application is running under. It returned blank for my service user as well (at least when attaching to the process).
任何其他建议?
推荐答案
在 Environment.SpecialFolder
枚举映射到的 CSIDL
,但没有 CSIDL
为 LocalLow
文件夹。所以,你必须使用 KNOWNFOLDERID
相反,使用 SHGetKnownFolderPath
API:
The Environment.SpecialFolder
enumeration maps to CSIDL
, but there is no CSIDL
for the LocalLow
folder. So you have to use the KNOWNFOLDERID
instead, with the SHGetKnownFolderPath
API:
void Main()
{
Guid localLowId = new Guid("A520A1A4-1780-4FF6-BD18-167343C5AF16");
GetKnownFolderPath(localLowId).Dump();
}
string GetKnownFolderPath(Guid knownFolderId)
{
IntPtr pszPath = IntPtr.Zero;
try
{
int hr = SHGetKnownFolderPath(knownFolderId, 0, IntPtr.Zero, out pszPath);
if (hr >= 0)
return Marshal.PtrToStringAuto(pszPath);
throw Marshal.GetExceptionForHR(hr);
}
finally
{
if (pszPath != IntPtr.Zero)
Marshal.FreeCoTaskMem(pszPath);
}
}
[DllImport("shell32.dll")]
static extern int SHGetKnownFolderPath( [MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);
这篇关于检测应用程序数据的位置\ LocalLow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!