OPC Studio User's Guide and Reference
GetWritableFileProvider Method
Example 



OpcLabs.EasyOpcUA Assembly > OpcLabs.EasyOpc.UA.FileTransfer Namespace > IEasyUAFileTransferExtension Class : GetWritableFileProvider Method
The OPC UA file transfer object that will perform the operation.
Endpoint descriptor. Identifies the OPC-UA server.
Node descriptor of the OPC UA directory.
Gets a writable file provider on the specified OPC UA directory.
Syntax
'Declaration
 
<ExtensionAttribute()>
<NotNullAttribute()>
Public Shared Function GetWritableFileProvider( _
   ByVal fileTransfer As IEasyUAFileTransfer, _
   ByVal endpointDescriptor As UAEndpointDescriptor, _
   ByVal directoryNodeDescriptor As UANodeDescriptor _
) As IWritableFileProvider
'Usage
 
Dim fileTransfer As IEasyUAFileTransfer
Dim endpointDescriptor As UAEndpointDescriptor
Dim directoryNodeDescriptor As UANodeDescriptor
Dim value As IWritableFileProvider
 
value = IEasyUAFileTransferExtension.GetWritableFileProvider(fileTransfer, endpointDescriptor, directoryNodeDescriptor)

Parameters

fileTransfer
The OPC UA file transfer object that will perform the operation.
endpointDescriptor
Endpoint descriptor. Identifies the OPC-UA server.
directoryNodeDescriptor
Node descriptor of the OPC UA directory.

Return Value

Returns a writable file provider for the specified OPC UA directory.
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.

Remarks

This method does not make any OPC UA operation, and does not throw the usual OpcLabs.EasyOpc.UA.OperationModel.UAException. You can therefore always safely use it to obtain the result interface. All OPC UA operations are made later, on the returned object, and also operation-related exceptions are thrown at that time.

A file provider is an abstraction of a file system, with access to its files and directories. Concrete implementations include a file provider for the physical file system (provided by the operating system), file providers for embedded resources, and, as in this case, a file provider for OPC UA File Transfer facility.

The returned writable file provider object allows modifications to the file system and file data. If you only need read-only operations with the file system and its files and directories, it is recommended that you obtain the Microsoft.Extensions.FileProviders.IFileProvider interface instead, by calling the GetFileProvider2 method. This way, you will be protected against calling a method that makes some modifications accidentally.

Accessing OPC UA files and directories over the abstract file provider model (based on Microsoft.Extensions.FileProviders.IFileProvider, Microsoft.Extensions.FileProviders.IFileInfo, OpcLabs.BaseLib.Extensions.FileProviders.IDirectoryContents2 and their "writable" counterparts), which is what this method does, is highly recommended over the other, lower-level methods. Once you obtained the generalized interface in the file provider model, operations on files and directories can be performed using just names and paths, without having to deal with OPC UA nodes and references.

Example

.NET

// Shows how to copy an OPC UA file, using the file provider model.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

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

namespace UADocExamples.FileProviders._WritableFileInfo
{
    class CopyTo
    {
        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 a file, and a directory. Then, copy the file into the directory.
            try
            {
                string fileName = "MyFile-" + random.Next();
                Console.WriteLine($"Creating file, '{fileName}'...");
                IWritableFileInfo writableFileInfo = writableFileProvider.GetWritableFileInfo(fileName);
                writableFileInfo.WriteAllBytes(Array.Empty<byte>());

                string directoryName = "MyDirectory-" + random.Next();
                Console.WriteLine($"Creating directory, '{directoryName}'...");
                IWritableDirectoryContents writableDirectoryContents = writableFileProvider.GetWritableDirectoryContents(directoryName);
                writableDirectoryContents.Create();

                Console.WriteLine("Copying the file...");
                writableFileInfo.CopyTo(FormattableString.Invariant($"{directoryName}/{fileName}"));
            }
            // 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 copy an OPC UA file, 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 .
# 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 a file, and a directory. Then, copy the file into the directory.
try:
    fileName = 'MyFile-' + str(random.randint(0, 999_999_999))
    print("Creating file, '", fileName, "'...", sep='')
    writableFileInfo = writableFileProvider.GetWritableFileInfo(fileName)
    IWritableFileInfoExtension.WriteAllBytes(writableFileInfo, Array.Empty[Byte]())

    directoryName = 'MyDirectory-' + str(random.randint(0, 999_999_999))
    print("Creating  directory, '", directoryName, "'...", sep='')
    writableDirectoryContents = writableFileProvider.GetWritableDirectoryContents(directoryName)
    writableDirectoryContents.Create()

    print('Copying the file...')
    IWritableFileInfoExtension.CopyTo(writableFileInfo, directoryName + '/' + fileName)

# 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.')
' Shows how to copy an OPC UA file, using the file provider model.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

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

Namespace FileProviders._WritableFileInfo

    Friend Class CopyTo

        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 a file, and a directory. Then, copy the file into the directory.
            Try
                Dim fileName As String = "MyFile-" & random.Next()
                Console.WriteLine($"Creating file, '{fileName}'...")
                Dim writableFileInfo As IWritableFileInfo = writableFileProvider.GetWritableFileInfo(fileName)
                writableFileInfo.WriteAllBytes(Array.Empty(Of Byte))

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

                Console.WriteLine("Copying the file...")
                writableFileInfo.CopyTo(FormattableString.Invariant($"{directoryName}/{fileName}"))

                ' 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
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