gpt4 book ai didi

c# - 检测 AppData\LocalLow 的位置

转载 作者:可可西里 更新时间:2023-11-01 08:38:21 24 4
gpt4 key购买 nike

我正在尝试找到 AppData\LocalLow 文件夹的路径。

我找到了一个例子,它使用:

string folder = "c:\users\" + Environment.UserName + @"\appdata\LocalLow";

其中一个与 c:users 相关联,这似乎有点脆弱。

我试过

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

但这给了我 AppData\Local,由于应用程序运行的安全限制,我需要 LocalLow。它也为我的服务用户返回空白(至少在附加到流程时)。

还有什么建议吗?

最佳答案

Environment.SpecialFolder 枚举映射到 CSIDL ,但是 LocalLow 文件夹没有 CSIDL。所以你必须使用 KNOWNFOLDERID相反,使用 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);

关于c# - 检测 AppData\LocalLow 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4494290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com