OPC Studio User's Guide and Reference
CopyFile(IEasyUAFileTransfer,UAEndpointDescriptor,UANodeDescriptor,UANodeId,UANodeId) Method
Example 



OpcLabs.EasyOpcUA Assembly > OpcLabs.EasyOpc.UA.FileTransfer Namespace > IEasyUAFileTransferExtension Class > CopyFile Method : CopyFile(IEasyUAFileTransfer,UAEndpointDescriptor,UANodeDescriptor,UANodeId,UANodeId) 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.
The node Id of the file to copy.
The node Id of the target directory of the copy operation.
Copies an OPC UA file to another directory.
Syntax
'Declaration
 
<ExtensionAttribute()>
<NotNullAttribute()>
Public Overloads Shared Function CopyFile( _
   ByVal fileTransfer As IEasyUAFileTransfer, _
   ByVal endpointDescriptor As UAEndpointDescriptor, _
   ByVal directoryNodeDescriptor As UANodeDescriptor, _
   ByVal existingFileNodeId As UANodeId, _
   ByVal newDirectoryNodeId As UANodeId _
) As UANodeId
'Usage
 
Dim fileTransfer As IEasyUAFileTransfer
Dim endpointDescriptor As UAEndpointDescriptor
Dim directoryNodeDescriptor As UANodeDescriptor
Dim existingFileNodeId As UANodeId
Dim newDirectoryNodeId As UANodeId
Dim value As UANodeId
 
value = IEasyUAFileTransferExtension.CopyFile(fileTransfer, endpointDescriptor, directoryNodeDescriptor, existingFileNodeId, newDirectoryNodeId)

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.
existingFileNodeId
The node Id of the file to copy.
newDirectoryNodeId
The node Id of the target directory of the copy operation.

Return Value

Returns the node Id of the copied object.
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.

The OPC UA operation has failed. This operation exception in uniformly used to allow common handling of various kinds of errors. The System.Exception.InnerException always contains information about the actual error cause.

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 internally uses the OPC UA File Transfer Method .

The directoryNodeDescriptor must be an existing node in the server, of object type OpcLabs.EasyOpc.UA.AddressSpace.Standard.UAObjectTypeIds.FileDirectoryType.

The existingFileNodeId must be an existing node in the server, of object type OpcLabs.EasyOpc.UA.AddressSpace.Standard.UAObjectTypeIds.FileType.

Recommendation: Whenever possible, do not use this method directly, and use a higher-level abstraction instead. You can obtain a writable file provider (using GetWritableFileProvider), and make use of its methods, and methods available on the writable directory contents (obtained using OpcLabs.BaseLib.Extensions.FileProviders.IWritableDirectory.GetWritableDirectoryContents) or writable file info (obtained using OpcLabs.BaseLib.Extensions.FileProviders.IWritableDirectory.GetWritableFileInfo).

Example

.NET

// Shows how to copy an OPC UA file, using the file transfer client.
// Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.AddressSpace;
using OpcLabs.EasyOpc.UA.Extensions;
using OpcLabs.EasyOpc.UA.FileTransfer;
using OpcLabs.EasyOpc.UA.OperationModel;

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

            // An object that aggregates an OPC UA file system.
            UANodeDescriptor objectDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files";

            // 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();

            // Create a file, and a directory. Then, copy the file into the directory.
            try
            {
                // The file system node is a root directory of the file system.
                Console.WriteLine("Getting file system...");
                UANodeDescriptor fileSystemNodeDescriptor = fileTransferClient.GetFileSystem(endpointDescriptor, objectDescriptor);

                string fileName = "MyFile-" + random.Next();
                Console.WriteLine($"Creating file, '{fileName}'...");
                UANodeId fileNodeId = fileTransferClient.CreateFile(endpointDescriptor, fileSystemNodeDescriptor, fileName);
                Console.WriteLine($"Node Id of the file: {fileNodeId}");

                string directoryName = "MyDirectory-" + random.Next();
                Console.WriteLine($"Creating directory, '{directoryName}'...");
                UANodeId directoryNodeId = fileTransferClient.CreateDirectory(endpointDescriptor, fileSystemNodeDescriptor, directoryName);
                Console.WriteLine($"Node Id of the directory: {directoryNodeId}");

                Console.WriteLine("Copying the file...");
                fileTransferClient.CopyFile(endpointDescriptor, fileSystemNodeDescriptor, fileNodeId, directoryNodeId);
            }
            catch (UAException uaException)
            {
                Console.WriteLine("*** Failure: {0}", uaException.GetBaseException().Message);
                return;
            }

            Console.WriteLine("Finished...");
        }
    }
}
# Shows how to copy an OPC UA file, using the file transfer client.
# Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
#
# 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 OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.Extensions import *
from OpcLabs.EasyOpc.UA.FileTransfer import *
from OpcLabs.EasyOpc.UA.OperationModel import *


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

# An object that aggregates an OPC UA file system.
objectDescriptor = UANodeDescriptor('nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files')

# 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)

# Create a file, and a directory. Then, copy the file into the directory.
try:
    # The file system node is a root directory of the file system.
    print('Getting file system...')
    fileSystemNodeDescriptor = IEasyUAFileTransferExtension.GetFileSystem(fileTransferClient,
                                                                          endpointDescriptor, objectDescriptor)

    fileName = 'MyFile-' + str(random.randint(0, 999_999_999))
    print("Creating file, '", fileName, "'...", sep='')
    fileNodeId = IEasyUAFileTransferExtension.CreateFile(fileTransferClient,
                                                         endpointDescriptor, fileSystemNodeDescriptor, fileName)
    print('Node Id of the file: ', fileNodeId, sep='')

    directoryName = 'MyDirectory-' + str(random.randint(0, 999_999_999))
    print("Creating directory, '", directoryName, "'...", sep='')
    directoryNodeId = fileTransferClient.CreateDirectory(endpointDescriptor,
                                                         fileSystemNodeDescriptor,
                                                         directoryName)
    print('Node Id of the directory: ', directoryNodeId, sep='')

    print('Copying the file...')
    IEasyUAFileTransferExtension.CopyFile(fileTransferClient,
                                          endpointDescriptor, fileSystemNodeDescriptor, fileNodeId, directoryNodeId)
    # If you want browse for directories, use the BrowseDirectories method instead.

except UAException as uaException:
    print('*** Failure: ' + uaException.GetBaseException().Message)
    exit()

print()
print('Finished.')
' Shows how to copy an OPC UA file, using the file transfer client.
' Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.AddressSpace
Imports OpcLabs.EasyOpc.UA.Extensions
Imports OpcLabs.EasyOpc.UA.FileTransfer
Imports OpcLabs.EasyOpc.UA.OperationModel

Namespace FileTransfer._EasyUAFileTransferClient

    Friend Class Copy

        Public Shared Sub File1()

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

            ' An object that aggregates an OPC UA file system.
            Dim objectDescriptor As UANodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files"

            ' 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

            ' Create a file, and a directory. Then, copy the file into the directory.
            Try
                ' The file system node is a root directory of the file system.
                Console.WriteLine("Getting file system...")
                Dim fileSystemNodeDescriptor As UANodeDescriptor = fileTransferClient.GetFileSystem(endpointDescriptor, objectDescriptor)

                Dim fileName As String = "MyFile-" & random.Next()
                Console.WriteLine($"Creating file, '{fileName}'...")
                Dim fileNodeId As UANodeId = fileTransferClient.CreateFile(endpointDescriptor, fileSystemNodeDescriptor, fileName)
                Console.WriteLine($"Node Id of the file: {fileNodeId}")

                Dim directoryName As String = "MyDirectory-" & random.Next()
                Console.WriteLine($"Creating directory, '{directoryName}'...")
                Dim directoryNodeId As UANodeId = fileTransferClient.CreateDirectory(endpointDescriptor, fileSystemNodeDescriptor, directoryName)
                Console.WriteLine($"Node Id of the directory: {directoryNodeId}")

                Console.WriteLine("Copying the file...")
                fileTransferClient.CopyFile(endpointDescriptor, fileSystemNodeDescriptor, fileNodeId, directoryNodeId)
            Catch uaException As UAException
                Console.WriteLine("*** Failure: {0}", uaException.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