Back to Devexpress

WorkspaceManager.SaveWorkspace(String, Object, Boolean) Method

windowsforms-devexpress-dot-utils-dot-workspacemanager-dot-saveworkspace-x28-system-dot-string-system-dot-object-system-dot-boolean-x29.md

latest7.9 KB
Original Source

WorkspaceManager.SaveWorkspace(String, Object, Boolean) Method

SECURITY-RELATED CONSIDERATIONS

Using file paths sourced from untrusted input may expose unauthorized files or allow unintended file access. Always validate and normalize all external paths to prevent path manipulation.

Saves a workspace from the WorkspaceManager.Workspaces collection to a local file.

Namespace : DevExpress.Utils

Assembly : DevExpress.Utils.v25.2.dll

NuGet Packages : DevExpress.Utils, DevExpress.Wpf.Core

Declaration

csharp
public bool SaveWorkspace(
    string name,
    object path,
    bool createIfNotExisting = false
)
vb
Public Function SaveWorkspace(
    name As String,
    path As Object,
    createIfNotExisting As Boolean = False
) As Boolean

Parameters

NameTypeDescription
nameString

The name of the target workspace in the WorkspaceManager.Workspaces collection.

| | path | Object |

The path to which the target workspace is to be saved.

|

Optional Parameters

NameTypeDefaultDescription
createIfNotExistingBooleanFalse

true to capture and then save the current controls’ layouts to a file if the WorkspaceManager.Workspaces collection does not contain a workspace with the specified name. false to interrupt the workspace saving if the WorkspaceManager.Workspaces collection does not contain a workspace with the specified name.

|

Returns

TypeDescription
Boolean

true if saving was successful; otherwise, false.

|

Example

The following code shows how to use the WorkspaceManager component to save the form’s bounds and state, and the layout of DevExpress controls when a form is closed, and load this layout when the form starts.

You may need to call the controls’ ForceInitialize methods (e.g., GridControl.ForceInitialize) before applying layouts to the controls in a Form.Load event handler.

csharp
string file = "layout.xml";
 string workspaceName1 = "MyLayout";

 private void Form1_Load(object sender, EventArgs e) {
     //Use the WorkspaceManager to handle the layout of DevExpress controls that reside within the current form.
     workspaceManager1.TargetControl = this;

     // Save & restore the form's size, position and state along with DevExpress controls' layouts.
     workspaceManager1.SaveTargetControlSettings = true;

     // Disable layout load animation effects 
     workspaceManager1.AllowTransitionAnimation = DevExpress.Utils.DefaultBoolean.False;

    // Disable (de)serialization for the following controls (if required):
    //WorkspaceManager.SetSerializationEnabled(gaugeControl1, false);
    //WorkspaceManager.SetSerializationEnabled(accordionControl1, false);

    // When restoring layouts of controls in a Form.Load event handler,
    // you may need to call the controls' ForceInitialize methods to finish their initialization before restoring their layouts.
    //gridControl1.ForceInitialize();
    //dockManager1.ForceInitialize();
    //barManager1.ForceInitialize();
    //...

     //Load DevExpress controls' layouts from a file
     if (workspaceManager1.LoadWorkspace(workspaceName1, file, true))
         workspaceManager1.ApplyWorkspace(workspaceName1);
 }

 private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
     //Save DevExpress controls' layouts to a file
     workspaceManager1.CaptureWorkspace(workspaceName1, true);
     workspaceManager1.SaveWorkspace(workspaceName1, file, true);
 }
vb
Dim file As String = "layout.xml"
Dim workspaceName1 As String = "MyLayout"

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Use the WorkspaceManager to handle the layout of DevExpress controls that reside within the current form.
    WorkspaceManager1.TargetControl = Me

    ' Save & restore the form's size, position and state along with DevExpress controls' layouts.
    WorkspaceManager1.SaveTargetControlSettings = True

    ' Disable layout load animation effects 
    WorkspaceManager1.AllowTransitionAnimation = DevExpress.Utils.DefaultBoolean.False

    ' Disable (de)serialization for the following controls (if required):
    'WorkspaceManager.SetSerializationEnabled(GaugeControl1, False)
    'WorkspaceManager.SetSerializationEnabled(AccordionControl1, False)

    ' When restoring layouts of controls in a Form.Load event handler,
    ' you may need to call the controls' ForceInitialize methods to finish their initialization before restoring their layouts.
    'GridControl1.ForceInitialize()
    'DockManager1.ForceInitialize()
    'BarManager1.ForceInitialize()
    '...

    ' Load DevExpress controls' layouts from a file
    If WorkspaceManager1.LoadWorkspace(workspaceName1, file, True) Then
        WorkspaceManager1.ApplyWorkspace(workspaceName1)
    End If
End Sub

Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
    ' Save DevExpress controls' layouts to a file
    WorkspaceManager1.CaptureWorkspace(workspaceName1, True)
    WorkspaceManager1.SaveWorkspace(workspaceName1, file, True)
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the SaveWorkspace(String, Object, Boolean) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-workspacemanager-saving-load-app-layout/CS/T190543/Form1.cs#L116

csharp
this.workspaceManager1.CaptureWorkspace(workspaceName);
    this.workspaceManager1.SaveWorkspace(this.workspaceManager1.Workspaces[0].Name, this.saveFileDialog1.FileName);
}

winforms-workspacemanager-saving-load-app-layout/VB/T190543/Form1.vb#L104

vb
workspaceManager1.CaptureWorkspace(workspaceName)
    workspaceManager1.SaveWorkspace(workspaceManager1.Workspaces(0).Name, saveFileDialog1.FileName)
End If

See Also

CaptureWorkspace(String, Boolean)

WorkspaceManager Class

WorkspaceManager Members

DevExpress.Utils Namespace