Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using DSOFile;
using System;
using System.Reflection;
using System.Windows.Forms;

/// <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
        {
            file = (OleDocumentProperties)Activator.CreateInstance(typeof(DSOFile.OleDocumentProperties));

            // Open file
            file.Open(filename, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
            comment = GetTagField(file);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in DSOFileExample : GetCustomPropertyValue " + ex.Message + " Stack trace " + ex.StackTrace);

            // Handle errors here
        }
        finally
        {
            if (file != null) file.Close(); // Clean up
        }

        return comment;
    }

    //public static void CreateDsoObj(string fileName)
    //{
    //    var oOleDocProp = CreateObject("");// new DSOFile.OleDocumentProperties();
    //    oOleDocProp.Open(fileName);
    //}

    static OleDocumentProperties file;
    static object Create()
    {
        // dynamically load assembly from file Test.dll
        Assembly testAssembly = Assembly.LoadFile(@"I:\ExcelHiddenContent\dsofile_x64.dll");
        // get type of class Calculator from just loaded assembly
        Type dsoType = testAssembly.GetType("DSOFile.OleDocumentProperties");

        // create instance of class Calculator
        return Activator.CreateInstance(dsoType);
    }
    /// <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();
        MessageBox.Show("Start Set tagging");
        try
        {
            file = new DSOFile.OleDocumentProperties();

        //file = (OleDocumentProperties)Activator.CreateInstance(typeof(DSOFile.OleDocumentPropertiesClass));

            file.Open(filename, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
            SetTagField(file, value);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in DSOFileExample : SetCustomPropertyValue " + ex.Message + " Stack trace " + ex.StackTrace);

            // 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();
    }
}


What I have tried:

I have tried with DSOFile.
But with 64 bit it is not working.
Need C# code snippet.

file = (OleDocumentProperties)Activator.CreateInstance(typeof(DSOFile.OleDocumentProperties));
part is throwing an exception
Posted
Updated 4-Jun-19 3:09am
v2
Comments
lmoelleb 4-Jun-19 7:17am    
That's a lot of code. Where did you find the suspicious parts when using the debugger?

1 solution

C++
/// 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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900