0

I completely frustrated with this issue and dot know how to solve it. I guess it happened after some windows update because it was working well before. My OS is WINDOWS 11 Pro 22H2 OS Build #22621.674 So the issue is then when you try to access any environment registered folder like %<FOLDER>% the C# code returns path in application root folder. Example

new DirectoryInfo("%TEMP%") //=>result: "<AppRootPath>\%TEMP%"

or

Path.GetFullPath("%USERPROFILE%") //=>result: "<AppRootPath>\%USERPROFILE%"

In my solution I have few settings pointed on the %temp% folder for dev environment and it is really annoing now to delete all these new %TEMP% folders in my solution directory after debugging.

What I found else it is when I try to execute 'cd %TEMP%' in command line it works like a sharm, but if only I do the same in powershel it returns me the folder not found error, at the same time 'cd $env:temp' works as expected.

I want to emphasize once again that it worked well before.

Hope some one of you can help me with this weird issue. Thank you in advance.

  • This is because `%TEMP%` and such are "autocompleted" by the shell, not by the OS/ filesystem itself. Take a look at [this](https://stackoverflow.com/q/867485/9363973) Q&A as well as [`Environment.GetFolderPath`](https://learn.microsoft.com/en-us/dotnet/api/system.environment.getfolderpath?view=net-6.0) and the [`Environment,SpecialFolder`](https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=net-7.0) enum. Alternatively use [`Environment.GetEnvironmentVariable`](https://learn.microsoft.com/en-us/dotnet/api/system.environment.getenvironmentvariable?view=net-7.0) – MindSwipe Nov 03 '22 at 10:37
  • Thanks @MindSwipe for the fast answer. It is clear that system or app should expand somehow that %TEMP% folder path, my emphasize was that it WAS WORKING BEFORE, maybe you know what changed in my system after update, so I can revert that stupid "fix" without uninstalling complete patch. It is not so easy to expand %TEMP% to real path if it is used as a setting path in 3rd party library. Yes I can use my own cusom preconfigured folder istead of %TEMP%, yes I can expand full path in all other places where I will write my code, but is here a real solution how to make it work as it was before? – user3043235 Nov 03 '22 at 11:05

1 Answers1

0

You could use Environment.ExpandEnvironmentVariables. From MSDN:

Replaces the name of each environment variable embedded in the specified string with the string equivalent of the value of the variable, then returns the resulting string.

In your code:

new DirectoryInfo(Environment.ExpandEnvironmentVariables("%TEMP%"))
EylM
  • 5,967
  • 2
  • 16
  • 28
  • It was only example of the code and the result for that code. I do not use in my code eiser DirectoryInfo or Path.GetFullPath. I set only the path value in the appsetting.json file and the value contains %TEMP% and app instead on creating result in user profile temp folder creates new folder in application root folder. – user3043235 Nov 03 '22 at 14:25
  • Ok, and who is using the %TEMP% value set in the appsetting.json ? – EylM Nov 03 '22 at 14:34
  • It is very comfortable to use %TEMP% on development environment to store there some logs or some caches. I do not have any issues with production it is only for development and it was working always like this but now it is stoope and all people want to sow me some extrantions i do not need any extractions i want to repare my environment to make it work like it was before update. – user3043235 Nov 03 '22 at 21:49