Setting File Attributes on Upload

A while back, I wrote an article on the extra steps you need to do on download in order to avoid the “edited out of turn” status .  Unfortunately, I neglected to provide the extra steps you need to do on upload.

When adding or checking in a file from the working folder, you should set the file to read-only.  This makes the file less likely to get updated by accident.  It also insures the correct file status in Vault Explorer. 

You should also set the create date on the local file so that it matches the CreateDate value from the File object returned from Vault.  This part is a bit confusing because File.CreateDate is the datetime that the version was created (aka. the check-in time).  So your file on disk will have a create date larger than the last modified date.

Those are the two basic things to set: read-only and create date.  They are same two steps are used for download.  Here is some example code:

// C#

File file = m_svcMgr.DocumentService.AddFile(

    folder.Id, filename, comment,

    System.IO.File.GetLastWriteTime(localpath),

    null, null, FileClassification.None, false, byteArray);

 

FileAttributes baseAttr = System.IO.File.GetAttributes(localpath);

FileAttributes rwAttr = baseAttr & ~FileAttributes.ReadOnly;

FileAttributes readOnlyAttr = baseAttr FileAttributes.ReadOnly;

 

// clear the read-only bit if it is there

System.IO.File.SetAttributes(localpath, rwAttr);

 

// set create time to match Vault

System.IO.File.SetCreationTime(localpath, file.CreateDate);

 

// set the read-only bit

System.IO.File.SetAttributes(localpath, readOnlyAttr);

' VB

Dim file As File = m_svcMgr.DocumentService.AddFile( _

    folder.Id, filename, comment, _

    System.IO.File.GetLastWriteTime(localpath), _

    Nothing, Nothing, _

    FileClassification.None, False, byteArray)

 

Dim baseAttr As FileAttributes = _

    System.IO.File.GetAttributes(localpath)

Dim rwAttr As FileAttributes = _

    baseAttr And Not FileAttributes.[ReadOnly]

Dim readOnlyAttr As FileAttributes = _

    baseAttr Or FileAttributes.[ReadOnly]

 

' clear the read-only bit if it is there

System.IO.File.SetAttributes(localpath, rwAttr)

 

' set create time to match Vault

System.IO.File.SetCreationTime(localpath, file.CreateDate)

 

' set the read-only bit

System.IO.File.SetAttributes(localpath, readOnlyAttr)

If you are uploading from a location other than the working folder, you don’t need to worry about these steps.  The Vault status will be empty since it won’t find a match on the local file system.  If you are checking in and keeping the file checked out, you can also ignore these steps. 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading