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

3

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\"

Finesse answered 11/7, 2019 at 7:44 Comment(3)
That's probably due to Revit defining its own %TMP/TEMP% Environment Variable.Iams
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"Finesse
Possible alternative would be: Path.Combine(System.Environment.GetEnvironmentVariable("LOCALAPPDATA"), "Temp")Iams
B
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.

Buffer answered 11/7, 2019 at 7:53 Comment(0)
N
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);
        }
Nadaha answered 11/7, 2019 at 8:0 Comment(0)
A
0
Path.Combine(
  Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
  "AppData",
  "Local",
  "Temp");
Ally answered 11/7, 2019 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.