Hide Table of Contents

Add Files to Vault Example (C#)

This example shows how to add files to a vault.

NOTE: If using the .NET Framework 4.0 primary interop assembly provided with SOLIDWORKS Enterprise PDM, see Using .NET Framework 4.0 in Stand-alone Applications.

//----------------------------------------------------------------------------
// Preconditions:
// 1. Start Microsoft Visual Studio 2010.
//    a. Click File > New > Project > Visual C# > Windows Forms Application.
//    b. Type AddFiles_CSharp in Name.
//    c. Click Browse and navigate to the folder where to create 
//       the project.
//    d. Click OK
//    e. Click Show All Files in the Solution Explorer toolbar and expand 
//       Form1.cs in the Solution Explorer.
//    f. Replace the code in Form1.cs with this code.
//    g. To create the form, replace the code in Form1.Designer.cs with this code.
// 2. Add EPDM.Interop.epdm.dll as a reference (right-click the project
//    name in the Solution Explorer, click Add Reference, click 
//    Assemblies > Framework in the left-side panel, browse to the top folder of 
//    your SOLIDWORKS Enterprise PDM installation, locate and click 
//    EPDM.Interop.epdm.dll, click Open, and click Add).
// 3. Add Microsoft.VisualBasic as a reference (click COM in the left-side panel, 
//    click Microsoft.VisualBasic, click Add, and click Close).

// 4. Right-click EPDM.Interop.epdm in References, click Properties, and set 
//    Embed Interop Types to False to handle methods that pass arrays of 
//    structures.
// 5. Ensure that a vault workflow with state, Waiting for Approval, exists.

// 6. Click Debug > Start Debugging or press F5.
//
// Postconditions: 
// 1. Displays the Add files to vault dialog box.
//    a. Select a vault view.
//    b. Click Browse
//       1. Locate and click three files outside the vault.
//       2. Click Open.
//    c. Click Add files.
//       1. Adds the selected files to the root folder of the vault.
//       2. Checks in the files.
//       3. Creates path, \temp\files, in the root folder.
//       4. Moves one of the files to subfolder, \temp\files.
//       5. Copies one of the files to subfolder, \temp.
//       6. Changes the state of one of the files in the root folder to
//          Waiting for Approval.
// 2. Observe the changes in the vault.

// 3. Close the Add files to vault dialog box.         
//----------------------------------------------------------------------------
 
//Form1.cs
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EPDM.Interop.epdm;
using Microsoft.VisualBasic;
 
 
namespace AddFiles_CSharp
{
    public partial class Form1 : FormIEdmCallback6
    {
        public Form1()
        {
            InitializeComponent();
        }
        private IEdmVault5 vault1 = null;
 
        public void Form1_Load(System.Object sender, System.EventArgs e)
        {
            try
            {
                IEdmVault5 vault1 = new EdmVault5();
                IEdmVault8 vault = (IEdmVault8)vault1;
                EdmViewInfo[] Views = null;
 
                vault.GetVaultViews(out Views, false);
                VaultsComboBox.Items.Clear();
                foreach (EdmViewInfo View in Views)
                {
                    VaultsComboBox.Items.Add(View.mbsVaultName);
                }
                if (VaultsComboBox.Items.Count > 0)
                {
                    VaultsComboBox.Text = (string)VaultsComboBox.Items[0];
                }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + " " + ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        public void BrowseButton_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                ListBox.Items.Clear();
 
                if (vault1 == null)
                {
                    vault1 = new EdmVault5();
                }
                if (!vault1.IsLoggedIn)
                {
                    //Log into selected vault as the current user
                    vault1.LoginAuto(VaultsComboBox.Text, this.Handle.ToInt32());
                }
 
                //Set the initial directory in the Open dialog
                OpenFileDialog.InitialDirectory = vault1.RootFolderPath;
                //Show the Open dialog
                System.Windows.Forms.DialogResult DialogResult;
                DialogResult = OpenFileDialog.ShowDialog();
                //If the user didn't click Open, exit
                if (!(DialogResult == System.Windows.Forms.DialogResult.OK))
                {
                    return;
                }
 
                foreach (string FileName in OpenFileDialog.FileNames)
                {
                    ListBox.Items.Add(FileName);
                }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + " " + ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

public void AddFiles_Click(System.Object sender, System.EventArgs e)
    {

 
try {
IEdmVault7 vault2 = null;
if (vault1 == null) {
vault1 = new EdmVault5();
}
vault2 = (IEdmVault7)vault1;
if (!vault1.IsLoggedIn) {
//Log into selected vault as the current user
vault1.LoginAuto(VaultsComboBox.Text, this.Handle.ToInt32());
}
 
if (!(ListBox.Items.Count == 3)) {
MessageBox.Show("Please click three files that are not in the vault.");
return;
}
 
IEdmFile6 aFile = default(IEdmFile6);
 
// Add selected files to the root folder of the vault
IEdmFolder6 Folder = default(IEdmFolder6);
Folder = (IEdmFolder6)vault2.RootFolder;
 
EdmAddFileInfo[] Files = new EdmAddFileInfo[3];
 
string[] FileNames = new string[3];
int Index = 0;
string fileStr = "";
foreach ( Object FileName_loopVariable in ListBox.Items) {
fileStr = FileName_loopVariable.ToString();
FileNames[Index] = fileStr.Substring(fileStr.LastIndexOf("\\"));
Index = Index + 1;
}
 
string Path = "";
Index = 0;
foreach (Object FileName_loopVariable in ListBox.Items) {
Path = FileName_loopVariable.ToString();
Files[Index].mbsPath = Path;
Files[Index].mlEdmAddFlags = (int)EdmAddFlag.EdmAdd_Simple;
Files[Index].mlFileID = 0;
Files[Index].mlSrcDocumentID = 0;
Files[Index].mlSrcProjectID = 0;
                Files[Index].mbsNewName = "";
Index = Index + 1;
}
 
Folder.AddFiles(this.Handle.ToInt32(), ref Files, this);
 
// Check in the files
IEdmFile6[] arrFiles = new IEdmFile6[4];
Index = 0;
            fileStr = "";
            IEdmFolder5 ppoRetParentFolder;
foreach (Object FileName_loopVariable in FileNames) {
                fileStr = FileName_loopVariable.ToString();
                aFile = (IEdmFile6)vault2.GetFileFromPath(Folder.LocalPath + "\\" + fileStr, out ppoRetParentFolder);
aFile.UnlockFile(this.Handle.ToInt32(), "");
arrFiles[Index] = aFile;
Index = Index + 1;
}
 
// Create \temp\files subfolder under the root folder
Folder.CreateFolderPath("\\temp\\files"this.Handle.ToInt32());
 
// Move one of the files to the \temp\files subfolder
IEdmFolder5 dest = default(IEdmFolder5);
dest = vault2.GetFolderFromPath(Folder.LocalPath + "\\temp\\files");
aFile = arrFiles[0];
aFile.Move(this.Handle.ToInt32(), Folder.ID, dest.ID, 0);
 
// Copy one of the files to the \temp subfolder
aFile = arrFiles[1];
aFile.GetFileCopy(this.Handle.ToInt32(), "", Folder.LocalPath + "\\temp\\");
 
// Change the state of one of the files to Waiting for Approval
aFile = arrFiles[2];
aFile.ChangeState("Waiting for Approval", Folder.ID"The file is waiting for approval."this.Handle.ToInt32(), (int)EdmStateFlags.EdmState_Simple);
 
catch (System.Runtime.InteropServices.COMException ex) {
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + " " + ex.Message);
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}

 
        private EdmMBoxResult IEdmCallback6_MsgBox(int lParentWnd, int lMsgID, string bsMsg, EdmMBoxType eType = 0L)
        {
            Interaction.MsgBox(bsMsg);
            return EdmMBoxResult.EdmMbr_OK;
        }
        EdmMBoxResult IEdmCallback6.MsgBox(int lParentWnd, int lMsgID, string bsMsg, EdmMBoxType eType )
        {
            return IEdmCallback6_MsgBox(lParentWnd, lMsgID, bsMsg, eType);
        }
 
        private void IEdmCallback6_Resolve(int lParentWnd, ref EdmCmdData[] ppoItems)
        {
        }
        void IEdmCallback6.Resolve(int lParentWnd, ref EdmCmdData[] ppoItems)
        {
            IEdmCallback6_Resolve(lParentWnd, ref ppoItems);
        }
 
        private bool IEdmCallback6_SetProgress(int lBarIndex, int lPos, string bsMsg)
        {
            return true;
        }
        bool IEdmCallback6.SetProgress(int lBarIndex, int lPos, string bsMsg)
        {
            return IEdmCallback6_SetProgress(lBarIndex, lPos, bsMsg);
        }
 
        private void IEdmCallback6_SetProgressRange(int lBarIndex, int lMax)
        {
        }
        void IEdmCallback6.SetProgressRange(int lBarIndex, int lMax)
        {
            IEdmCallback6_SetProgressRange(lBarIndex, lMax);
        }
 
        private void IEdmCallback6_SetStatusMessage(int lBarIndex, string bsMessage)
        {
        }
        void IEdmCallback6.SetStatusMessage(int lBarIndex, string bsMessage)
        {
            IEdmCallback6_SetStatusMessage(lBarIndex, bsMessage);
        }
    }
}

 

Back to top

//Form1.Designer.cs

namespace AddFiles_CSharp
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.VaultsLabel = new System.Windows.Forms.Label();
            this.VaultsComboBox = new System.Windows.Forms.ComboBox();
            this.BrowseButton = new System.Windows.Forms.Button();
            this.ListBox = new System.Windows.Forms.ListBox();
            this.AddFiles = new System.Windows.Forms.Button();
            this.OpenFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.SuspendLayout();
            //
            //VaultsLabel
            //
            this.VaultsLabel.AutoSize = true;
            this.VaultsLabel.Location = new System.Drawing.Point(13, 26);
            this.VaultsLabel.Name = "VaultsLabel";
            this.VaultsLabel.Size = new System.Drawing.Size(94, 13);
            this.VaultsLabel.TabIndex = 0;
            this.VaultsLabel.Text = " Select vault view:";
            //
            //VaultsComboBox
            //
            this.VaultsComboBox.FormattingEnabled = true;
            this.VaultsComboBox.Location = new System.Drawing.Point(16, 42);
            this.VaultsComboBox.Name = "VaultsComboBox";
            this.VaultsComboBox.Size = new System.Drawing.Size(121, 21);
            this.VaultsComboBox.TabIndex = 1;
            //
            //BrowseButton
            //
            this.BrowseButton.Location = new System.Drawing.Point(16, 85);
            this.BrowseButton.Name = "BrowseButton";
            this.BrowseButton.Size = new System.Drawing.Size(98, 23);
            this.BrowseButton.TabIndex = 3;
            this.BrowseButton.Text = "Browse...";
            this.BrowseButton.UseVisualStyleBackColor = true;
            this.BrowseButton.Click +=new System.EventHandler(BrowseButton_Click);
            //
            //ListBox
            //
            this.ListBox.FormattingEnabled = true;
            this.ListBox.HorizontalScrollbar = true;
            this.ListBox.Location = new System.Drawing.Point(16, 114);
            this.ListBox.Name = "ListBox";
            this.ListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
            this.ListBox.Size = new System.Drawing.Size(259, 43);
            this.ListBox.TabIndex = 4;
            //
            //AddFiles
            //
            this.AddFiles.Location = new System.Drawing.Point(84, 178);
            this.AddFiles.Name = "AddFiles";
            this.AddFiles.Size = new System.Drawing.Size(98, 23);
            this.AddFiles.TabIndex = 5;
            this.AddFiles.Text = "Add files";
            this.AddFiles.UseVisualStyleBackColor = true;
            this.AddFiles.Click +=new System.EventHandler(AddFiles_Click);
            //
            //OpenFileDialog
            //
            this.OpenFileDialog.Multiselect = true;
            this.OpenFileDialog.Title = "Open";
            //
            //Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(317, 215);
            this.Controls.Add(this.AddFiles);
            this.Controls.Add(this.ListBox);
            this.Controls.Add(this.BrowseButton);
            this.Controls.Add(this.VaultsComboBox);
            this.Controls.Add(this.VaultsLabel);
            this.Name = "Form1";
            this.Text = "Add files to vault";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        internal System.Windows.Forms.Label VaultsLabel;
        internal System.Windows.Forms.ComboBox VaultsComboBox;
        internal System.Windows.Forms.Button BrowseButton;
        internal System.Windows.Forms.ListBox ListBox;
        internal System.Windows.Forms.Button AddFiles;
        internal System.Windows.Forms.OpenFileDialog OpenFileDialog;
    }
}

 

 

Back to top



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Add Files to Vault Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:



x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.