Table of Contents

Class FileUpdate

Namespace
Dynamicweb.Updates
Assembly
Dynamicweb.Core.dll

Represents an update that writes the contents from a provided stream to a specified file.

using System.Reflection;

namespace Dynamicweb.Updates.Examples
{
    /// <summary>
    /// Example UpdateProvider demonstrating usage of SqlUpdate, SettingUpdate, MethodUpdate, and FileUpdate.
    /// </summary>
    public class SqlUpdateSampleProvider : UpdateProvider
    {
        public override IEnumerable<Update> GetUpdates()
        {
            // SQL Update: Add a column to a table if it does not exist
            yield return SqlUpdate.AddColumn(
                id: "d50467a2-0e9b-4c70-a490-7ab4af88851b",
                provider: this,
                tableName: "MyCustomTable",
                columnName: "MyCustomColumn",
                columnDefinition: "NVARCHAR(50) NULL"
            );

            // SettingUpdate: Set a configuration value, only if it does not already exist
            yield return new SettingUpdate(
                id: "99fd48e3-f364-4108-bb19-6355e8b31896",
                provider: this,
                name: "/Globalsettings/System/Url/HighVolumeProductUrls/Enable",
                value: "True"
            );

            // SettingUpdate: Overwrite a configuration value even if it exists
            yield return new SettingUpdate(
                id: "89c68710-c2c1-4e02-8ea5-f712adeea24f",
                provider: this,
                name: "/Globalsettings/System/Url/HighVolumeProductUrls/Enable",
                value: "True",
                overwrite: true
            );

            // MethodUpdate: Execute custom logic during update
            yield return new MethodUpdate(
                id: "30c561dd-5509-4636-b81a-146be8a51e40",
                provider: this,
                method: context =>
                {
                    // Custom logic here
                    Console.WriteLine("Custom method executed as part of update.");
                }
            );

            // FileUpdate: Write a file to disk, only if it does not already exist
            yield return new FileUpdate(
                id: "efe5952c-bf3c-400d-a654-6fe2eb79ca44",
                provider: this,
                filePath: "/Files/Templates/Feeds/CustomFeedTemplate.cshtml",
                fileStreamProvider: () => GetFileStreamFromEmbeddedResource("CustomFeedTemplate.cshtml"),
                overwrite: false
            );

            // FileUpdate: Overwrite an existing file
            yield return new FileUpdate(
                id: "27c24328-285b-4cae-b62b-23130b6fcde5",
                provider: this,
                filePath: "/Files/Templates/Feeds/CustomFeedTemplate.cshtml",
                fileStreamProvider: () => GetFileStreamFromEmbeddedResource("CustomFeedTemplate.cshtml"),
                overwrite: true
            );
        }

        private Stream GetFileStreamFromEmbeddedResource(string resourceName)
        {
            string resourceFullName = $"CustomAssembly.{resourceName}";
            var assembly = Assembly.GetAssembly(this.GetType());
            var stream = assembly?.GetManifestResourceStream(resourceName);

            if (stream is null)
                throw new FileNotFoundException($"Embedded resource '{resourceFullName}' not found.");

            return stream;
        }
    }
}
public class FileUpdate : Update
Inheritance
FileUpdate
Inherited Members

Constructors

FileUpdate(string, UpdateProvider, string, Func<Stream>)

Initializes a new instance of the FileUpdate class. Writes the contents from the provided stream to the specified file path.

public FileUpdate(string id, UpdateProvider provider, string filePath, Func<Stream> fileStreamProvider)

Parameters

id string

The unique identifier for the update.

provider UpdateProvider

The update provider that owns this update.

filePath string

The relative path of the file to write to.

fileStreamProvider Func<Stream>

A delegate that returns a Stream containing the data to write to the file. The stream will be disposed after writing.

FileUpdate(string, UpdateProvider, string, Func<Stream>, bool)

Initializes a new instance of the FileUpdate class. Writes the contents from the provided stream to the specified file path, optionally overwriting an existing file.

public FileUpdate(string id, UpdateProvider provider, string filePath, Func<Stream> fileStreamProvider, bool overwrite)

Parameters

id string

The unique identifier for the update.

provider UpdateProvider

The update provider that owns this update.

filePath string

The relative path of the file to write to.

fileStreamProvider Func<Stream>

A delegate that returns a Stream containing the data to write to the file. The stream will be disposed after writing.

overwrite bool

If true, the file will be overwritten if it already exists; if false, the update will be skipped if the file exists.

Methods

Execute(UpdateContext)

Executes the update by writing the contents from the provided stream to the specified file. If Dynamicweb.Updates.FileUpdate.Overwrite is false and the file already exists, the update is skipped. The target directory is created if it does not exist.

public override void Execute(UpdateContext context)

Parameters

context UpdateContext

The context for the update execution.

To top