This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Set Static .NET Properties

System.Environment.CurrentDirectory Example

This example shows how to set a static property using the NET.setStaticProperty function.

The CurrentDirectory property in the System.Environment class is a static, read/write property. The following code creates a folder, temp, in the current folder and changes the CurrentDirectory property to the new folder.

Set your current folder to a specific path, for example:


Set the CurrentDirectory property:

saveDir = System.Environment.CurrentDirectory;
newDir = [char(saveDir) '\temp'];
ans = 

To restore the original CurrentDirectory value, type:


Do Not Use ClassName.PropertyName Syntax for Static Properties

This example shows how to mistakenly create a struct array instead of setting a class property.

If you use the ClassName.PropertyName syntax to set a static property, MATLAB® creates a struct array.

The following code creates a structure named System:

saveDir = System.Environment.CurrentDirectory;
newDir = [char(saveDir) '\temp'];
System.Environment.CurrentDirectory = newDir;
  Name         Size            Bytes  Class

  System       1x1               376  struct
  newDir       1x12               24  char
  saveDir      1x1               112  System.String

Try to use a member of the System namespace.

oldDate = System.DateTime(1992,3,1);
Reference to non-existent field 'DateTime'.

To restore your environment, type:

clear System
Was this topic helpful?