Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / January 2005

Tip: Looking for answers? Try searching our database.

Reading File Summary in .NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Oueis - 01 Nov 2004 22:23 GMT
I'm trying to read the summary information of a NTFS file using .NET.
You know when you right click on a file and go to properties, you have
a bunch of tabs one of which is summary. I would like to be able to
write information there and be able to read it. I've done tons of
research and have found out how to do it using IPropertyStorage and
etc. I am able to write to that area of the file but cannot read. When
i try to read, i always get a blank even though the summary is full.
Here is my complete code, please help.

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace StructuredStorageWrapper
{
       public enum SummaryPropId : int
    {
        Title               =   0x00000002,
        Subject             =   0x00000003,
        Author              =   0x00000004,
        Keywords            =   0x00000005,
        Comments            =   0x00000006,
        Template            =   0x00000007,
        LastSavedBy         =   0x00000008,
        RevisionNumber      =   0x00000009,
        TotalEditingTime    =   0x0000000A,
        LastPrinted         =   0x0000000B,
        CreateDateTime      =   0x0000000C,
        LastSaveDateTime    =   0x0000000D,
        NumPages            =   0x0000000E,
        NumWords            =   0x0000000F,
        NumChars            =   0x00000010,
        Thumbnail           =   0x00000011,
        AppName             =   0x00000012,
        Security            =   0x00000013
    }

    public enum STGC : int
    {
        DEFAULT         =   0,
        OVERWRITE       =   1,
        ONLYIFCURRENT   =   2,
        DANGEROUSLYCOMMITMERELYTODISKCACHE = 4,
        CONSOLIDATE     =   8
    }

    public enum PROPSETFLAG : int
    {
        DEFAULT         =   0,
        NONSIMPLE       =   1,
        ANSI            =   2,
        UNBUFFERED      =   4,
        CASE_SENSITIVE  =   8
    }

    public enum STGM : int
    {
        READ                =   0x00000000,
        WRITE               =   0x00000001,
        READWRITE           =   0x00000002,
        SHARE_DENY_NONE     =   0x00000040,
        SHARE_DENY_READ     =   0x00000030,
        SHARE_DENY_WRITE    =   0x00000020,
        SHARE_EXCLUSIVE     =   0x00000010,
        PRIORITY            =   0x00040000,
        CREATE              =   0x00001000,
        CONVERT             =   0x00020000,
        FAILIFTHERE         =   0x00000000,
        DIRECT              =   0x00000000,
        TRANSACTED          =   0x00010000,
        NOSCRATCH           =   0x00100000,
        NOSNAPSHOT          =   0x00200000,
        SIMPLE              =   0x08000000,
        DIRECT_SWMR         =   0x00400000,
        DELETEONRELEASE     =   0x04000000
    }

    public enum STGFMT : int
    {
        STORAGE         =   0,
        FILE            =   3,
        ANY             =   4,
        DOCFILE         =   5
    }

    [StructLayout(LayoutKind.Explicit, Size=8, CharSet=CharSet.Unicode)]
    public struct PropSpec
    {
        [FieldOffset(0)] public int ulKind;
        [FieldOffset(4)] public IntPtr Name_Or_ID;
    }

    [StructLayout(LayoutKind.Explicit, Size=16)]
    public struct PropVariant
    {
        [FieldOffset(0)] public short variantType;
        [FieldOffset(8)] public IntPtr pointerValue;
        [FieldOffset(8)] public byte byteValue;
        [FieldOffset(8)] public long longValue;

        public void FromObject(object obj)
        {
            if (obj.GetType() == typeof(string))
            {
                this.variantType = (short)VarEnum.VT_LPWSTR;
                this.pointerValue = Marshal.StringToHGlobalUni((string)obj);
            }
        }
    }

    [ComVisible(true), ComImport(),
    Guid("0000013A-0000-0000-C000-000000000046"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IPropertySetStorage
    {
        uint Create(
            [In] ref System.Guid rfmtid,
            [In] IntPtr pclsid,
            [In] int grfFlags,
            [In] int grfMode,
            ref IPropertyStorage propertyStorage);

        int Open(
            [In] ref System.Guid rfmtid,
            [In] int grfMode,
            [Out] out IPropertyStorage propertyStorage);
    }

    [ComVisible(true), ComImport(),
    Guid("00000138-0000-0000-C000-000000000046"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IPropertyStorage
    {
        int ReadMultiple(
            uint numProperties,
            [MarshalAs(UnmanagedType.LPArray)] PropSpec[]
propertySpecifications,
            [MarshalAs(UnmanagedType.LPArray)] PropVariant[] propertyValues);

        int WriteMultiple(
            uint numProperties,
            [MarshalAs(UnmanagedType.Struct)] ref PropSpec
            propertySpecification,
            ref PropVariant propertyValues,
            int propIDNameFirst);

        uint Commit(
            int commitFlags);
    }

    public enum HResults : uint
    {
        S_OK                        =   0,
        STG_E_FILEALREADYEXISTS     =   0x80030050
    }

    public class ole32
    {
        [StructLayout(LayoutKind.Explicit, Size=12,
            CharSet=CharSet.Unicode)]
            public struct STGOptions
        {
            [FieldOffset(0)] ushort usVersion;
            [FieldOffset(2)] ushort reserved;
            [FieldOffset(4)] uint uiSectorSize;
            [FieldOffset(8), MarshalAs(UnmanagedType.LPWStr)] string
                pwcsTemplateFile;
        }

        [DllImport("ole32.dll", CharSet=CharSet.Unicode)]
        public static extern uint StgCreateStorageEx(
            [MarshalAs(UnmanagedType.LPWStr)] string name,
            int accessMode, int storageFileFormat, int fileBuffering,
            IntPtr options, IntPtr reserved, ref System.Guid riid,
            [MarshalAs(UnmanagedType.Interface)] ref IPropertySetStorage
            propertySetStorage);

        [DllImport("ole32.dll", CharSet=CharSet.Unicode)]
        public static extern uint StgOpenStorageEx(
            [MarshalAs(UnmanagedType.LPWStr)] string name,
            int accessMode, int storageFileFormat, int fileBuffering,
            IntPtr options, IntPtr reserved, ref System.Guid riid,
            [MarshalAs(UnmanagedType.Interface)] ref IPropertySetStorage
            propertySetStorage);
    }

    public class FileSummary
    {
        private FileInfo m_fileInfo;
        public FileSummary(FileInfo fileInfo)
        {
            m_fileInfo = fileInfo;
            //m_fileInfo.Open(FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite);

        }

        public void SetProperty(SummaryPropId summaryPropertID,string value)
        {           
            // Open the file and its property set stream
            IPropertySetStorage propSetStorage = null;
            Guid IID_PropertySetStorage = new
                Guid("0000013A-0000-0000-C000-000000000046");

            uint hresult = ole32.StgOpenStorageEx(m_fileInfo.FullName,               
                (int)(STGM.SHARE_EXCLUSIVE | STGM.READWRITE),
                (int)STGFMT.FILE,
                0,
                (IntPtr)0,
                (IntPtr)0,
                ref IID_PropertySetStorage,
                ref propSetStorage);           

            // Open the Summary Information property set
            Guid fmtid_SummaryProperties = new
                Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9");
            IPropertyStorage propStorage = null;

            int hresult2 = propSetStorage.Open(
                ref fmtid_SummaryProperties,               
                (int)(STGM.SIMPLE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE),
                out propStorage);

            // Assemble a property descriptor for the specified property
            PropSpec propertySpecification = new PropSpec();
            propertySpecification.ulKind = 1;
            propertySpecification.Name_Or_ID = new
                IntPtr((int)summaryPropertID);

            //Set the value wanted in a property variant
            PropVariant propertyValue = new PropVariant();
            propertyValue.FromObject(value);

            // Pass the property spec and its new value to the    WriteMultiple
method
            propStorage.WriteMultiple(1, ref propertySpecification, ref
                propertyValue, 0);

            // Commit Changes
            hresult = propStorage.Commit((int)STGC.DEFAULT);           

            // Release the Com objects
            Marshal.ReleaseComObject(propStorage);   
            Marshal.ReleaseComObject(propSetStorage);
            propStorage = null;       
            propSetStorage = null;

        }

        public string GetProperty(SummaryPropId summaryPropertID)
        {
            // Open the file and its property set stream
            IPropertySetStorage propSetStorage = null;
            Guid IID_PropertySetStorage = new
                Guid("0000013A-0000-0000-C000-000000000046");

            uint hresult = ole32.StgOpenStorageEx(m_fileInfo.FullName,               
                (int)(STGM.DIRECT_SWMR | STGM.READ | STGM.SHARE_DENY_NONE),
                (int)STGFMT.FILE,
                0,
                System.IntPtr.Zero,
                System.IntPtr.Zero,
                ref IID_PropertySetStorage,
                ref propSetStorage);

            // Open the Summary Information property set
            Guid fmtid_SummaryProperties = new
                Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9");
            IPropertyStorage propStorage = null;

           
            int hresult2 = propSetStorage.Open(
                ref fmtid_SummaryProperties,               
                (int)(STGM.DIRECT | STGM.SHARE_EXCLUSIVE),
                out propStorage);
           
            // Specify the property to be retrieved
            PropSpec[] propSpecs = new PropSpec[1];
            propSpecs[0].ulKind = 1;
            propSpecs[0].Name_Or_ID = new IntPtr((int)summaryPropertID);
            PropVariant[] propertyValues = new PropVariant[1];
           
            // Retrieve the value           
            hresult2 = propStorage.ReadMultiple(1, propSpecs, propertyValues);
           
            // Release the Com objects
            Marshal.ReleaseComObject(propStorage);   
            Marshal.ReleaseComObject(propSetStorage);
            propStorage = null;       
            propSetStorage = null;
           
            return Marshal.PtrToStringUni(propertyValues[0].pointerValue);           

        }
   
    }
}

// This should go in your startup class
static void Main()
{
FileSummary summary = new FileSummary(new
System.IO.FileInfo(@"c:\log.txt"));
summary.SetProperty(SummaryPropId.Title,"I can do whatever i want
here");
// That works
MessageBox.Show(summary.GetProperty(SummaryPropId.Title));
//This gives me blank
}

Thanks in advance

Mark
Mark Oueis - 11 Nov 2004 15:33 GMT
Can anyone help? Anyone? Maybe direct me to someone who can?

Mark
Aviad - 05 Jan 2005 12:15 GMT
Hi Mark,

I suggest to you to download this Microsoft file: dsoFile.dll

Look at: http://support.microsoft.com/?id=224351

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.