Avatar billede cdull Nybegynder
17. maj 2005 - 13:07 Der er 5 kommentarer og
1 løsning

Arbejde med Egenskaber af filer

Tænkte på en lille ting.
Hvordan arbejder man med filer og deres egenskaber.

Vil som eksempel bruge en mp3fil.
Der er jo et punkt under Egenskaber/Properties som på engelsk er Summary. Der står en masse informationer som man selv kan rette manuelt.
Spørgsmålet er så hvordan man får tilgang til disse egenskaber gennem programmering i C#.
Dette kan gælde alle slags filer og ikke kun for mp3.

Der må vel være en oversigt over hvordan de forskellige ting tilgås eller en beskrivelse af selv samme.

På Forhånd Tak.
Savo.
Avatar billede nielle Nybegynder
17. maj 2005 - 21:31 #1
Hej,

Umiddelbart ser det ikke ud til at være direkte muligt i .NET 1.1 - måske i 2.0?

Efter en intens Googling fandt jeg til sidst denne her:

http://groups-beta.google.com/group/microsoft.public.dotnet.framework/msg/99a5dd5c80c084b5?q=%22structured+storage%22+c%23&start=10&hl=en&lr=&ie=UTF-8&oe=UTF-8&rnum=17

Nedenstående kode sætter værdien af feltet "Kommentarer", men samtidigt bliver de andre felter nulstillet, så der er da noget som skal kigges på lige der.

Men nu har du noget at gå i gang med:

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

using StructuredStorageWrapper;

namespace Eksperten
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            string DesktopDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            string EkspertenTxtFile = Path.Combine(DesktopDir, "Eksperten.txt");

            // first you need to either create or open a file and its
            // property set stream

            Guid IID_PropertySetStorage = new Guid("0000013A-0000-0000-C000-000000000046");
            IPropertySetStorage propSetStorage = null;

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

            // next you need to create or open the Summary Information property set

            Guid fmtid_SummaryProperties = new Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9");
            IPropertyStorage propStorage = null;

            hresult = propSetStorage.Create(
                ref fmtid_SummaryProperties,
                (IntPtr)0,
                (int)PROPSETFLAG.DEFAULT,
                (int)(STGM.CREATE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE),
                ref propStorage);

            // next, you assemble a property descriptor for the property you
            // want to write to, in our case the Comment property

            PropSpec propertySpecification = new PropSpec();
            propertySpecification.ulKind = 1;
            propertySpecification.Name_Or_ID = new IntPtr((int)SummaryPropId.Comments);

            // now, set the value you want in a property variant

            PropVariant propertyValue = new PropVariant();
            propertyValue.FromObject("Test Comment");

            // Simply pass the property spec and its new value to the WriteMultiple
            // method and you're almost done

            propStorage.WriteMultiple(1, ref propertySpecification, ref propertyValue, 2);

            // the only thing left to do is commit your changes. Now you're done!
            hresult = propStorage.Commit((int)STGC.DEFAULT);
        }
    }
}

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, MarshalAs(UnmanagedType.Struct)] ref System.Guid rfmtid,
            [In] IntPtr pclsid,
            [In] int grfFlags,
            [In] int grfMode,
            ref IPropertyStorage propertyStorage);

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

    [ComVisible(true), ComImport(),
    Guid("00000138-0000-0000-C000-000000000046"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IPropertyStorage
    {
        int ReadMultiple(
            uint numProperties,
            PropSpec[] propertySpecifications,
            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);
    }
}
Avatar billede burningice Nybegynder
18. maj 2005 - 19:06 #2
få faen for en masse interop-kode :/
Avatar billede nielle Nybegynder
18. maj 2005 - 20:09 #3
Ja det er rimeligt vildt! Gad vide om opgaven bliver .NET-venlig med 2.0?
Avatar billede nielle Nybegynder
23. maj 2005 - 19:23 #4
cdull> Har du fået kigget på det?
Avatar billede cdull Nybegynder
24. maj 2005 - 13:38 #5
Rigtig nice.
Nej har endnu ikke fået kigget ordentligt på det, sidder pt. med nogle andre opgaver.
Denne opgave går jeg igang med om noget tid.
Men smid et svar for har kigget det lidt igennem og det ser meget godt ud.
Avatar billede nielle Nybegynder
24. maj 2005 - 23:04 #6
Svar :^)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester