Microsoft Dynamics NAV: Create and Delete Directory dotNET Interop

The ability to extend the development library through dotNET Interoperability within the development environment of Microsoft Dynamics NAV really does open up things up. I have been asked on several occasions about creating directories  via Dynamics NAV code. Directories can easily be created and deleted through the use of the System.IO.Directory class.  The CreateDirectory method allows you to create all directories and subdirectories in the specified path.  This makes creating nested directories rather easy. The DeleteDirectory method also allows for the recursive removal of directories.

Text Constants:

Name ConstValue
DirectoryExistsErr Directory already exists.
DirectoryDoesNotExistErr Directory does not exist.
DirectoryCreatedMsg Directory %1 created.
DirectoryDeletededMsg Directory deleted.


Name DataType Subtype
SystemIODirectory DotNet System.IO.Directory.'mscorlib'
SystemIODirectoryInfo DotNet System.IO.DirectoryInfo.'mscorlib'


LOCAL CreateDirectory(path : Text;showconfirmation : Boolean) : Boolean
	IF SystemIODirectory.Exists(path) THEN
	SystemIODirectoryInfo := SystemIODirectory.CreateDirectory(path);
	IF showconfirmation THEN BEGIN

LOCAL DeleteDirectory(path : Text;recursive : Boolean;showconfirmation : Boolean)
	IF NOT SystemIODirectory.Exists(path) THEN
	SystemIODirectory.Delete(path, recursive);
	IF showconfirmation THEN BEGIN
		IF NOT SystemIODirectory.Exists(path) THEN



I have attached the sample codeunit Cod50051.txt (2.2KB)

Comments (1) -

Thank you Brad,
Especially for including the sample codeunit. It worked right away.
Further I would like to add that your code is beautiful and easy to read.
Cheers, Finn

Add comment

Reading Stuff

Information in this document subject to change without notice.
All Software source code published is for demonstration and knowledge sharing purposes only. The Code is supplied "as is" without warranty as to result, performance or merchantability. Use at your own risk.
The opinions expressed herein are the opinions of the author and do not reflect those of any other entity.