6

I am currently working on a plugin for AutoCAD that allows users to interact with a document versioning application, and in order to sync files between the remote repository and local machine, I had planned on using custom file properties. The properties would be set when a file is initially downloaded, and then persisted for as long as the file remains on the user's local drive. I am not really interested in an AutoCAD-specific solution, because my plugin will deal with files other than AutoCAD drawings (text files, image files, among others). Therefore, I want a library that can handle as many potential file types as possible.

When searching for how to implement this kind of thing in C#, I almost immediately came across the DSOFile library. Everything I read said it was designed for MS Office, but that it should work with any file, as long as the file system is NTFS (at least that's my understanding). I had no problem setting custom properties on files such as plain-text documents (.txt), AutoCAD drawings (.dwg), and images (.jpg, .tif, etc). However, I noticed that once any of these files were saved, the custom properties were wiped out. The only case in which I saw custom properties were persisted after saving, were on MS Office documents. I figured this issue was related to the application that I was using to save the files (AutoCAD, MS Paint, notepad, etc), but I can't be 100% sure of that. Before I decide to go with a solution other than using DSOFile, I wanted to see if anyone on SO had some insight in to this issue.

I tested using my own code and using the demo that comes with DSOFile, and saw the same result both times. Custom properties were wiped out after saving any type of file other than an MS Office (Word and Excel) document.

Here is an example similar to the code I would use to add a new custom property...

var docProperties = new OleDocumentProperties();
docProperties.Open("myfile.txt", false, dsoFileOpenOptions.dsoOptionDefault);

try
{
  object value = "some value";
  docProperties.CustomProperties.Add("MyCustomProp", ref value);
}

finally
{
  docProperties.Close(true); // save and close
}
Justin Holzer
  • 2,326
  • 2
  • 22
  • 22
  • DSOFile is meant for MS Office files only - it can't be used for others. – Todd Main May 22 '10 at 15:49
  • It turns out that it can be, but that in order for them to be persisted the application saving the file must specifically ensure those properties are saved. Go ahead and try out the demo program that ships with DSOfile on a non-office file and you will see that it has no problem getting or setting properties for any file type. As far as I know, the storage mechanism for the properties is part of the NTFS file system and has nothing to with office. Office just happens to use those properties and it explicitly saves their values when it saves a file, whereas apps like Notepad will not. – Justin Holzer May 24 '10 at 18:25
  • So you can save custom properties to a non-Office document and have them persist? Are AutoCAD files saved in the OLE Structured Storage format? Can you post on how you do that? – Todd Main Jun 19 '10 at 20:51
  • To my knowledge, all files saved on an NTFS partition are saved in this format. The thing is that any time the files are saved, the OLE properties must be set, otherwise, they're wiped out. There's nothing special about MS Office vs AutoCAD. The difference is that MS Office apps save the OLE file properties every time you save a file, whereas AutoCAD does not. You could certainly write an add-on for AutoCAD that makes use of the DSOFile library and saves OLE properties any time an AutoCAD drawing is saved. To see for yourself, use the demo app that ships with DSOFile on a non office file. – Justin Holzer Jun 22 '10 at 15:37
  • 4
    It should also be noted that OLE file properties are also wiped out whenever you move a file to a different file system (storage format). For instance, if you put a file in to an archive (zip, tar, etc), burn it to a CD/DVD, email as an attachment, copy to a non-NTFS shared drive, etc, the OLE file properties will be wiped out. – Justin Holzer Jun 22 '10 at 15:45

1 Answers1

1

This may be too late but I've used this a bit or Autodesk Revit RFA files as well as PDF files and it works fine. You can't edit them while the RFA is open though.

Did you call docProperties.Save() at all?

RodH257
  • 3,552
  • 5
  • 36
  • 46
  • As far as I can tell, it seems to be dependent on the application that is saving the file. As previously mentioned, I had no issues setting the properties for any file. The only time I had any problems is when I saved a file using AutoCAD (more specifically, the 2010 version). Whatever AutoCAD is doing when it saves a drawing, it's wiping out those property values. It may very well work with Revit, Acrobat, or any other number of apps. – Justin Holzer Sep 09 '10 at 13:23
  • ah I see, in my case I'm hooking into the document closed event in Revit and re-rewriting the properties I desire each time, so even if Revit does wipe the properties like AutoCAD did for you, it will re-add them. I don't think I've had an instance of me using DSOFile where that has been the case, so I haven't run into your problem! When I get the chance I might see if Revit does it, just out of interest. – RodH257 Sep 10 '10 at 05:33