Path.GetTempPath() 方法 returns 使用 Revit 2020 时最后带有 GUID 的 UserTempPath

Path.GetTempPath() method returns UserTempPath with GUID in the end when using Revit 2020

大多数应用程序使用我的加载项 return "C:\Users\[username]\AppData\Local\Temp\" 路径。但是一个应用程序是 returning "C:\Users\[username]\AppData\Local\Tempaffa5dd-2f26-4c96-9965-7a78f5c76321\"。每次启动应用程序时,最后的 GUID 都会改变。

我 运行 我的加载项来自的应用程序是 Revit 2015-2020。 Revit 版本 2015-2019 return 正确的路径。但是 Revit 2020 是 return 在最后附加了 GUID 的路径。代码保持不变。

    public static string GetLocalFilePath(string sourceUri, string fileName, string extension)
    {
        string[] sasTokenSeparated = sourceUri.Split('?');
        string[] uriParts = sasTokenSeparated[0].Split('/');
        string documentId = uriParts[uriParts.Length - 2];
        documentId = documentId.Split('.')[0];
        string extensionWithDot = string.Empty;
        if (!extension.StartsWith("."))
        {
            extensionWithDot = "." + extension;
        }
        else
        {
            extensionWithDot = extension;
        }
        string localPath = Path.Combine(Path.GetTempPath(), documentId, fileName + fileExtension);
        return localPath;
    }

我期待这条路, "C:\Users\[username]\AppData\Local\Temp\"

虽然我实际上正在寻找路径, "C:\Users\[username]\AppData\Local\Tempaffa5dd-2f26-4c96-9965-7a78f5c76321\"

根据 this forum linkRevit 2020 根据您所看到的改变返回值。

Since Revit 2020 the requested temp path contains an additional guid at the end of the path, which changes after every restart of Revit(ie. C:\Users\USERNAME\AppData\Local\Tempae8c0d-197b-4b44-b8d3-8823fabbba4f). It seems like Revit changes the temp path for the scope of the application.

我做了一个小修复,用 '\' 字符拆分路径并组成一个字符串直到单词 'Temp',它有效但将其视为一个概念 .

private void concept()
        {
            string fullpath = Path.GetTempPath();
            string[] ph = fullpath.Split('\');
            bool fix = false;
            string fixedpath = "";
            foreach (string word in ph)
            {

                if (fix == false)
                {
                    fixedpath = fixedpath + word + @"\";
                }
                if (word.ToLower().Equals("temp"))
                {
                    fix = true;
                }

            }
            MessageBox.Show(fixedpath);
        }
Path.Combine(
  Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
  "AppData",
  "Local",
  "Temp");