using System;
using DSOFile;
/// <summary>
/// (c) GinkoSolutions.com
/// This sample class enables you to set meta file information within physical files.
/// This is similar to EXIF information for picture files.
/// DSOFile works for every file type, not just office files.
///
/// NOTE
/// DsoFile is an unmnaged 32bit dll. We need to compile in x86 mode or we get 'class not registered exception'
/// There is a third party 64bit version available online, or recompile the C++ source manually.
/// </summary>
public static class DSOFileExample
{
/// <summary>
/// A property name that this sample code uses to store tag information.
/// </summary>
private static string FILE_PROPERTY = "CustomFileTag";
/// <summary>
/// Gets value stored in a custom tag
/// </summary>
/// <param name="filename">Path to the file</param>
/// <returns>Our custom value stored in the custom file tag</returns>
public static string GetCustomPropertyValue(string filename)
{
string comment = string.Empty;
OleDocumentProperties file
= new DSOFile
.OleDocumentProperties();
try
{
// Open file
file.Open(filename, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
comment = GetTagField(file);
}
catch (Exception ex)
{
// Handle errors here
}
finally
{
if (file != null) file.Close(); // Clean up
}
return comment;
}
/// <summary>
/// Sets value stored in a files custom tag
/// </summary>
/// <param name="filename">Path to the file</param>
/// <param name="value">Value to store in the custom file tag</param>
public static void SetCustomPropertyValue(string filename, string value)
{
OleDocumentProperties file
= new DSOFile
.OleDocumentProperties();
try
{
file.Open(filename, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
SetTagField(file, value);
}
catch (Exception ex)
{
// Handle errors here
}
finally // Always called, even when throwing in Exception block
{
if (file != null) file.Close(); // Clean up
}
}
/// <summary>
/// Gets the value of the file tag property
/// </summary>
/// <param name="file">Ole Document File</param>
/// <returns>Contents of the file tag property. Can be null or empty.</returns>
private static string GetTagField(DSOFile.OleDocumentProperties file)
{
string result = string.Empty;
foreach (DSOFile.CustomProperty property in file.CustomProperties)
{
if (property.Name == FILE_PROPERTY) // Check property exists
{
result = property.get_Value();
break;
}
}
return result;
}
/// <summary>
/// Sets the value of the file tag property
/// </summary>
/// <param name="file">Ole Document File</param>
/// <param name="value">Value to set as the property value</param>
/// <param name="saveDocument">Saves document if set to true</param>
/// <param name="closeDocument">Closes the document if set to true</param>
private static void SetTagField(DSOFile.OleDocumentProperties file, string value, bool saveDocument = true, bool closeDocument = true)
{
bool found = false;
foreach (DSOFile.CustomProperty property in file.CustomProperties)
{
if (property.Name == FILE_PROPERTY) // Check property exists
{
property.set_Value(value);
found = true;
break;
}
}
if (!found)
file.CustomProperties.Add(FILE_PROPERTY, value);
if (saveDocument)
file.Save();
if (closeDocument)
file.Close();
}
}