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.

Variables:

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

Methods:

LOCAL CreateDirectory(path : Text;showconfirmation : Boolean) : Boolean
	IF SystemIODirectory.Exists(path) THEN
		ERROR(DirectoryExistsErr);
		
	SystemIODirectoryInfo := SystemIODirectory.CreateDirectory(path);
	IF showconfirmation THEN BEGIN
		MESSAGE(STRSUBSTNO(DirectoryCreatedMsg,SystemIODirectoryInfo.Name));
	END;

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

Use:

CreateDirectory('C:\temp\a\b',TRUE);
DeleteDirectory('C:\temp\a',TRUE,TRUE);

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

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.