how to change the working directory and then change it back?

I am using an a dll to access R. it needs to change the working directory inorder to function. Unity then throws a fatal error saying if i change the working directory i need to change it back. I do not know how to change it back. how do I capture the original working directory and then set it back with the script after i am done running the R code?

note:other post encountering this issue remove an unneeded dll and restart unity. this is avoidance rather than solution. how to control working directory from script is needed, because there was intent to change the working directory to the one noted.

fatal error: the current working directory was changed from your Unity project folder located at ‘C:/development/unity/r_dll02’ to ‘C:/PROGRA~1/R/R-34~1.0’. This is not allowed! If you are setting the current working directory from script temporarily, then please make sure to set it back to the Unity project folder immediately after you are done.

How do i do as it suggest and change it back when i am done???

I ran into this post while trying to solve this same issue. The System.IO namespace has some handy functions that let you get the current working directory and set the current working directory for your app. Here’s my solution.

string oldDirectory  = Directory.GetCurrentDirectory();
// Your code here
Directory.SetCurrentDirectory(oldDirectory);