我正在尝试查找AppData\LocalLow文件夹的路径。
我找到了一个使用以下命令的示例:
string folder = "c:\users\" + Environment.UserName + @"\appdata\LocalLow";例如,它与c:和users捆绑在一起,这似乎有点脆弱。
我试着用
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)但是这给了我AppData\Local,由于应用程序在其下运行的安全约束,我需要LocalLow。对于我的服务用户,它也返回空白(至少在附加到进程时是这样)。
还有其他建议吗?
发布于 2010-12-21 07:45:20
Environment.SpecialFolder枚举映射到CSIDL,但LocalLow文件夹没有CSIDL。因此,您必须通过SHGetKnownFolderPath应用编程接口使用KNOWNFOLDERID:
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);发布于 2022-02-03 05:32:40
Thomas的答案是有效的,但对于某些用例来说是不必要的复杂。
一个快速的解决方案是:
string LocalLowPath =
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).Replace("Roaming","LocalLow");https://stackoverflow.com/questions/4494290
复制相似问题