OPC Studio User's Guide and Reference
CreateSubdirectory Method
Example 



OpcLabs.BaseLib Assembly > OpcLabs.BaseLib.Extensions.FileProviders Namespace > IWritableDirectoryContentsExtension Class : CreateSubdirectory Method
The writable directory contents object that will perform the operation.

The value of this parameter cannot be null (Nothing in Visual Basic).

The path of the subdirectory to be created.

The value of this parameter cannot be null (Nothing in Visual Basic).

Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the IWritableDirectoryContents.
Syntax
'Declaration
 
<ExtensionAttribute()>
<NotNullAttribute()>
Public Shared Function CreateSubdirectory( _
   ByVal writableDirectoryContents As IWritableDirectoryContents, _
   ByVal path As String _
) As IWritableDirectoryContents
'Usage
 
Dim writableDirectoryContents As IWritableDirectoryContents
Dim path As String
Dim value As IWritableDirectoryContents
 
value = IWritableDirectoryContentsExtension.CreateSubdirectory(writableDirectoryContents, path)

Parameters

writableDirectoryContents
The writable directory contents object that will perform the operation.

The value of this parameter cannot be null (Nothing in Visual Basic).

path
The path of the subdirectory to be created.

The value of this parameter cannot be null (Nothing in Visual Basic).

Return Value

Returns the writable directory contents for the subdirectory that has been created.

This method never returns null (Nothing in Visual Basic).

Exceptions
ExceptionDescription

A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

An I/O error has occurred.

This is an operation error that depends on factors external to your program, and thus cannot be always avoided. Your code must handle it appropriately.

An invoked method is not supported at all, or is not supported with the parameters used to create the object.

A security error was detected.

This is an operation error that depends on factors external to your program, and thus cannot be always avoided. Your code must handle it appropriately.

The operating system has denied access because of an I/O error or a specific type of security error.

This is an operation error that depends on factors external to your program, and thus cannot be always avoided. Your code must handle it appropriately.

Remarks

This method is similar in purpose to System.IO.DirectoryInfo.CreateSubdirectory(System.String), but is generalized for any kind of writable directory contents, not just files in the physical file system.

Any and all directories specified in path are created, unless some part of path is invalid or the creation fails. The path parameter specifies a directory path, not a file path. If the subdirectory already exists, this method does nothing.

For Microsoft.Extensions.FileProviders.IDirectoryContents, IDirectory2, IDirectoryContents2, IWritableDirectory and IWritableDirectoryContents, relative paths are based on the directory itself; absolute paths are based on the root directory of the file provider. Notice the difference from Microsoft.Extensions.FileProviders.IFileInfo, IFileInfo2 and IWritableFileInfo, where relative paths are based on the directory where the file is located.

This is an extension method (info: C#, VB.NET). In languages that have support for extensions methods (such as C# and VB.NET), you can use the extension method as if it were a regular method on the object that is its first parameter. In other languages (such as with Python.NET), you will call the extension as a static method, and pass it the object on which it acts as its first parameter.

Example
// Shows how to create and delete OPC UA directories, using the file provider model.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.BaseLib.Extensions.FileProviders;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.Extensions;
using OpcLabs.EasyOpc.UA.FileTransfer;

namespace UADocExamples.FileProviders._WritableDirectoryContents
{
    class CreateAndDelete
    {
        public static void Main1()
        {
            // Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            var endpointDescriptor = new UAEndpointDescriptor("opc.tcp://localhost:48030")
                .WithUserNameIdentity("john", "master");

            // A node that represents an OPC UA file system (a root directory).
            UANodeDescriptor fileSystemNodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.FileSystem";

            // Create a random number generator - will be used for file/directory names.
            var random = new Random();
            
            // Instantiate the file transfer client object
            var fileTransferClient = new EasyUAFileTransferClient();

            Console.WriteLine("Getting writable file provider...");
            IWritableFileProvider writableFileProvider =
                fileTransferClient.GetWritableFileProvider(endpointDescriptor, fileSystemNodeDescriptor);
            // From this point onwards, the code is independent of the concrete realization of the file provider, and would
            // be identical e.g. for files in the physical file system, if the corresponding file provider was used.

            // Create two directories, and one nested directory, and delete the first one.
            try
            {
                string directoryName1 = "MyDirectory1-" + random.Next();
                Console.WriteLine($"Creating first directory, '{directoryName1}'...");
                IWritableDirectoryContents writableDirectoryContents1 = writableFileProvider.GetWritableDirectoryContents(directoryName1);
                writableDirectoryContents1.Create();

                string directoryName2 = "MyDirectory2-" + random.Next();
                Console.WriteLine($"Creating second directory, '{directoryName2}'...");
                IWritableDirectoryContents writableDirectoryContents2 = writableFileProvider.GetWritableDirectoryContents(directoryName2);
                writableDirectoryContents2.Create();

                string nestedDirectoryName = "MyDirectory3-" + random.Next();
                Console.WriteLine($"Creating nested directory, '{nestedDirectoryName}'...");
                writableDirectoryContents2.CreateSubdirectory(nestedDirectoryName);

                // At this moment, the directory structure we have created looks like this:
                // * MyDirectory1
                // * MyDirectory2
                // * * MyDirectory3

                Console.WriteLine("Deleting the first directory...");
                writableDirectoryContents1.Delete();
            }
            // Methods in the file provider model throw IOException and other exceptions, but not UAException.
            catch (Exception exception)
            {
                Console.WriteLine($"*** Failure: {exception.GetBaseException().Message}");
                return;
            }

            Console.WriteLine("Finished...");
        }
    }
}
' Shows how to create and delete OPC UA directories, using the file provider model.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports OpcLabs.BaseLib.Extensions.FileProviders
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.Extensions
Imports OpcLabs.EasyOpc.UA.FileTransfer

Namespace FileProviders._WritableDirectoryContents

    Friend Class CreateAndDelete

        Public Shared Sub Main1()

            ' Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            Dim endpointDescriptor As UAEndpointDescriptor =
                New UAEndpointDescriptor("opc.tcp://localhost:48030") _
                .WithUserNameIdentity("john", "master")

            ' A node that represents an OPC UA file system (a root directory).
            Dim fileSystemNodeDescriptor As UANodeDescriptor =
                "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.FileSystem"

            ' Create a random number generator - will be used for file/directory names.
            Dim random = New Random

            ' Instantiate the file transfer client object
            Dim fileTransferClient = New EasyUAFileTransferClient

            Console.WriteLine("Getting writable file provider...")
            Dim writableFileProvider As IWritableFileProvider =
                fileTransferClient.GetWritableFileProvider(endpointDescriptor, fileSystemNodeDescriptor)
            ' From this point onwards, the code is independent of the concrete realization of the file provider, and would
            ' be identical e.g. for files in the physical file system, if the corresponding file provider was used.

            ' Create two directories, and one nested directory, and delete the first one.
            Try
                Dim directoryName1 As String = "MyDirectory1-" & random.Next()
                Console.WriteLine($"Creating first directory, '{directoryName1}'...")
                Dim writableDirectoryContents1 As IWritableDirectoryContents = writableFileProvider.GetWritableDirectoryContents(directoryName1)
                writableDirectoryContents1.Create()

                Dim directoryName2 As String = "directoryName2-" & random.Next()
                Console.WriteLine($"Creating second directory, '{directoryName2}'...")
                Dim writableDirectoryContents2 As IWritableDirectoryContents = writableFileProvider.GetWritableDirectoryContents(directoryName2)
                writableDirectoryContents2.Create()

                Dim nestedDirectoryName As String = "MyDirectory3-" & random.Next()
                Console.WriteLine($"Creating nested directory, '{nestedDirectoryName}'...")
                writableDirectoryContents2.CreateSubdirectory(nestedDirectoryName)

                ' At this moment, the directory structure we have created looks Like this
                ' * MyDirectory1
                ' * MyDirectory2
                ' * * MyDirectory3

                Console.WriteLine("Deleting the first directory...")
                writableDirectoryContents1.Delete()

                ' Methods in the file provider model throw IOException and other exceptions, but not UAException.
            Catch exception As Exception
                Console.WriteLine("*** Failure: {0}", exception.GetBaseException.Message)
                Exit Sub
            End Try

            Console.WriteLine("Finished...")
        End Sub
    End Class
End Namespace
# Shows how to create and delete OPC UA directories, using the file provider model.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
# a commercial license in order to use Online Forums, and we reply to every post.
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import random

# Import .NET namespaces.
from System import *
from OpcLabs.BaseLib.Extensions.FileProviders import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.Extensions import *
from OpcLabs.EasyOpc.UA.FileTransfer import *


# Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe).
endpointDescriptor = UAEndpointDescriptor('opc.tcp://localhost:48030')
endpointDescriptor = UAEndpointDescriptorExtension.WithUserNameIdentity(endpointDescriptor,'john', 'master')

# A node that represents an OPC UA file system (a root directory).
fileSystemNodeDescriptor = UANodeDescriptor('nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.FileSystem')

# Create a random number generator - will be used for file/directory names.
random = random.Random()

# Instantiate the file transfer client object.
fileTransferClient = EasyUAFileTransferClient()

# Prevent prompt to trust the server certificate (INSECURE, used just for smooth example flow).
EasyUAClient.SharedParameters.EngineParameters.CertificateAcceptancePolicy.TrustEndpointUrlString(
    endpointDescriptor.UrlString)

print('Getting writable file provider...')
writableFileProvider = IEasyUAFileTransferExtension.GetWritableFileProvider(fileTransferClient,
                                                                            endpointDescriptor,
                                                                            fileSystemNodeDescriptor)
# From this point onwards, the code is independent of the concrete realization of the file provider, and would
# be identical e.g. for files in the physical file system, if the corresponding file provider was used.

# Create two directories, and one nested directory, and delete the first one.
try:
    directoryName1 = 'MyDirectory1-' + str(random.randint(0, 999_999_999))
    print("Creating first directory, '", directoryName1, "'...", sep='')
    writableDirectoryContents1 = writableFileProvider.GetWritableDirectoryContents(directoryName1)
    writableDirectoryContents1.Create()

    directoryName2 = 'MyDirectory2-' + str(random.randint(0, 999_999_999))
    print("Creating second directory, '", directoryName2, "'...", sep='')
    writableDirectoryContents2 = writableFileProvider.GetWritableDirectoryContents(directoryName2)
    writableDirectoryContents2.Create()

    nestedDirectoryName = 'MyDirectory3-' + str(random.randint(0, 999_999_999))
    print("Creating nested directory, '", nestedDirectoryName, "'...", sep='')
    IWritableDirectoryContentsExtension.CreateSubdirectory(writableDirectoryContents2, nestedDirectoryName)

    # At this moment, the directory structure we have created looks like this:
    # * MyDirectory1
    # * MyDirectory2
    # * * MyDirectory3

    print('Deleting the first directory...')
    IWritableDirectoryContentsExtension.Delete(writableDirectoryContents1)

# Methods in the file provider model throw IOException and other exceptions, but not UAException.
except Exception as exception:
    print('*** Failure: ' + exception.GetBaseException().Message)
    exit()

print()
print('Finished.')
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also

Reference

IWritableDirectoryContentsExtension Class
IWritableDirectoryContentsExtension Members
Create Method
OpcLabs.BaseLib.Extensions.FileProviders.IWritableDirectoryContentsExtension.Delete(OpcLabs.BaseLib.Extensions.FileProviders.IWritableDirectoryContents)