OPC Studio User's Guide and Reference
IWritableDirectoryContentsExtension Class
Members  Example 



View with Navigation Tools
OpcLabs.BaseLib Assembly > OpcLabs.BaseLib.Extensions.FileProviders Namespace : IWritableDirectoryContentsExtension Class
Contains extension methods to the IWritableDirectoryContents interface.
Syntax
'Declaration
 
<ExtensionAttribute()>
<ComVisibleAttribute(False)>
<ExceptionContractAnnotationAttribute(True)>
<ExceptionContractVerificationAttribute(True)>
Public MustInherit NotInheritable Class IWritableDirectoryContentsExtension 
 
'Usage
 
Dim instance As IWritableDirectoryContentsExtension
Remarks

This class contains extension methods (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...");
        }
    }
}
Inheritance Hierarchy

System.Object
   OpcLabs.BaseLib.Extensions.FileProviders.IWritableDirectoryContentsExtension

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