.NET Based OPC UA Client/Server SDK  3.1.0.500
FileModel Example

The FileModel class implements support for the FileType object on the server side, which can be used to give an OPC UA client access to a file managed by the OPC UA server.

The .NET OPC UA Demo Server contains an example in the Address Space in the folder Objects → Demo → 014_Files. The example code can be found in the Visual Studio Solution for the Demo Server in the file Demo → DemoNodeManager.File.cs.

The following paragraphs describe the method SetupFileObject which creates a new file object. It takes the following arguments:

parentId
The NodeId of the parent node. The created File object will be referenced by an Organizes reference.
browseName
The browse name of the new File object. The browseName is also used to create the NodeId of the File object.
filePath
The file path on the disk.
description
Used for the description attribute of the new file object.

Create the OPC UA Nodes

NodeId objectId = new NodeId("Demo.Files." + browseName, DefaultNamespaceIndex);
CreateObjectSettings settings = new CreateObjectSettings()
{
ParentNodeId = parentId,
ReferenceTypeId = ReferenceTypeIds.Organizes,
RequestedNodeId = objectId,
BrowseName = new QualifiedName(browseName, DefaultNamespaceIndex),
TypeDefinitionId = ObjectTypeIds.FileType
};
if (description != null)
{
settings.Description = new LocalizedText(description);
}
CreateObject(Server.DefaultRequestContext, settings);

Create the Model Class

UnifiedAutomation.UaServer.FileModel

FileModel file = new FileModel();

Set the FileModel Properties

file.Writable = true;
file.UserWritable = true;
file.MaxFileSize = 10000;
file.FileOnDisk = new System.IO.FileInfo(filePath);

Create a Default File if None Exists

if (!file.FileOnDisk.Exists)
{
using (var ostrm = file.FileOnDisk.Open(System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None))
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(
"This file is used for demonstrating the OPC UA FileType.\n"
+ "You can call the Open method of the TextFile object with Mode argument 1 for ReadOnly.\n"
+ "You have to remember the returned FileHandle argument.\n"
+ "This FileHandle can be used for reading the file by calling the Read method and closing\n"
+ "the file by calling the Close method.");
ostrm.Write(bytes, 0, bytes.Length);
}
file.FileOnDisk.Refresh();
}

Set the File Size Depending on the File Content

file.Size = (ulong)file.FileOnDisk.Length;

LinkModelToNode

UnifiedAutomation.UaServer.BaseNodeManager.LinkModelToNode

LinkModelToNode(objectId, file, null, null, 500);