简体   繁体   中英

C# Temp Folder with session IDs

I have an application that target .NET Framework 4.8. When using %temp% in File Explorer on a remote desktop machine I go to a path similar like this

C:\Users\UserName\Local\Temp\6 where 6 is the session ID.

My problem is that System.IO.Path.GetTempPath() method returns this value without the session ID. In .NET 6 however it does return the value with the session ID.

Upgrading the project to .NET 6 is currently out of the question so I was wondering how else I could get around this?

My proposed solution is to determine if in a remote session using

[DllImport("user32")]
static extern bool GetSystemMetric(int indeX);
const int SM_REMOTESESSION = 0x1000;

var isRemoteSession = GetSystemMetrics(SM_REMOTESESSION);

and then I can combine the two using Path.Combine()

Is there any better solutions out there?

I don't have a remote machine to test out but I have used Environment.ExpandEnvironmentVariables to get full paths for strings containing path tokens.

See if that works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM