8

Most of the applications consuming my add-in return "C:\Users\[username]\AppData\Local\Temp\" path. But one application is returning "C:\Users\[username]\AppData\Local\Temp\1affa5dd-2f26-4c96-9965-7a78f5c76321\". The GUID in the end changes every time I launch the application.

The application that I am running my add-in from are Revit 2015-2020. Revit versions 2015-2019 return the correct path. But Revit 2020 is returning the path with GUID appended in the end. The code remains the same.

    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;
    }

I am expecting the path, "C:\Users\[username]\AppData\Local\Temp\"

While I am actually getting path, "C:\Users\[username]\AppData\Local\Temp\1affa5dd-2f26-4c96-9965-7a78f5c76321\"

mjwills
  • 23,389
  • 6
  • 40
  • 63
Umar
  • 478
  • 3
  • 10
  • 1
    That's probably due to Revit defining its own `%TMP/TEMP%` Environment Variable. – haim770 Jul 11 '19 at 07:48
  • 1
    I have checked in Watch Window. The values that I have pasted are being returned from Path.GetTempPath() method. DocumentId is appended after the value returned from that method. localPath value in the above is "C:\Users\umar.aslam\AppData\Local\Temp\1affa5dd-2f26-4c96-9965-7a78f5c76321\4f5dce0b-e8d4-a4ea-1285-7765fd82fde1" – Umar Jul 11 '19 at 07:49
  • 1
    Possible alternative would be: `Path.Combine(System.Environment.GetEnvironmentVariable("LOCALAPPDATA"), "Temp")` – haim770 Jul 11 '19 at 07:55

3 Answers3

8

As per this forum link, Revit 2020 alters the value returned as per what you are seeing.

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\Temp\84ae8c0d-197b-4b44-b8d3-8823fabbba4f). It seems like Revit changes the temp path for the scope of the application.

mjwills
  • 23,389
  • 6
  • 40
  • 63
2

I made an small fix wich splits the Path by the '\' character and composes a string until the word 'Temp', it works but consider it a concept.

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);
        }
Umar
  • 478
  • 3
  • 10
icrescenti
  • 21
  • 3
0
Path.Combine(
  Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
  "AppData",
  "Local",
  "Temp");
Daniel Fisher lennybacon
  • 3,865
  • 1
  • 30
  • 38