[Closed] expanding file paths using .Net
I needed a fucntion that would expand filepath strings that contain windows environment variables
like “%temp% emp_diffuse.bmp” or “%programFiles%\myConversionUtility\myConversionUtility.exe”
So I used dotnet like so:
fn expandFilePath filePath=(
env=dotnetclass "System.Environment"
return env.expandEnvironmentVariables filePath
)
and calling it in other functions like so
openBitmap (expandFilePath "%temp% emp_diffuse.bmp" )
My question is: Is instantiating the System.Environment class every time I use this function bad practice? Should I rather instantiate once and call many times? I’m not sure what the cost difference is.
Defining function like this(your example) you only create one instance
fn expandFilePath filePath = ((dotnetclass "System.Environment").expandEnvironmentVariables filePath )
bmap = openBitmap (expandFilePath "%temp% emp_diffuse.bmp" )
/*close bmap*/ --free memory
the best practice is
expandFilePath = (dotnetclass "System.Environment").expandEnvironmentVariables
openBitmap (expandFilePath "%temp% emp_diffuse.bmp")
Just to be clear though, you’re not instanciating anything. It’s a static class. At worse, you’re making maxscript resolve the namespace more than necessary.
if you have access to the sdk you can check for yourself as the source code can be found in maxsdk\samples\maxscript\mxsdotnet\ if you have visual studio you could build the project as hybrid and run under the debugger as see exactly whats what :).
looking at the source theres considerable difference between
DotNetObjectManaged::Create
&
DotNetClassManaged::Create