本文介绍了如何使用IPropertySetStorage获取文件属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我在工作中继承的一些代码:
public string GetProperty(SummaryPropId summaryPropertID) { IPropertySetStorage pssSetStorage = null; Guid guidPropertySetStorage = new Guid("0000013A-0000-0000-C000-000000000046"); Guid guidSummaryProperties = new Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9"); IPropertyStorage psStorage = null; PropSpec[] pscSpecs = new PropSpec[1]; PropVariant[] pvValues = new PropVariant[1]; try { //Exit if file does not exist if (!fiSource.Exists) return null; // Open the file and its property set stream uint hresult = ole32.StgOpenStorageEx(fiSource.FullName, (int)(STGM.DIRECT_SWMR | STGM.READ | STGM.SHARE_DENY_NONE), (int)STGFMT.FILE, 0, System.IntPtr.Zero, System.IntPtr.Zero, ref guidPropertySetStorage, ref pssSetStorage); // Open the Summary Information property set int hresult2 = pssSetStorage.Open(ref guidSummaryProperties, (int)(STGM.DIRECT | STGM.SHARE_EXCLUSIVE), out psStorage); // Specify the property to be retrieved pscSpecs[0].ulKind = 1; pscSpecs[0].Name_Or_ID = new IntPtr((int)summaryPropertID); // Retrieve the value hresult2 = psStorage.ReadMultiple(1, pscSpecs, pvValues); // Release the Com objects Marshal.ReleaseComObject(psStorage); Marshal.ReleaseComObject(pssSetStorage); psStorage = null; pssSetStorage = null; } catch (Exception excException) { AuditMethodError(excException, System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " - " + excException.StackTrace); } finally { pssSetStorage = null; psStorage = null; pscSpecs = null; pvValues = null; } return Marshal.PtrToStringUni(pvValues[0].pointerValue); }
问题是,当到达行int hresult2 = pssSetStorage.Open(ref guidSummaryProperties, (int)(STGM.DIRECT | STGM.SHARE_EXCLUSIVE), out psStorage)
时,我收到此异常:
excException = {对象引用未设置为对象的实例."}
这是我的接口声明:
[ComVisible( true ),ComImport(),Guid(" 0000013A-0000-0000-C000-000000000046"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 公共 界面 IPropertySetStorage { uint 创建([In] ref System.Guid rfmtid,[In] IntPtr pclsid,[In] int grfFlags,[In] int grfMode, ref IPropertyStorage propertyStorage); int 打开([In] ref System.Guid rfmtid,[In] int grfMode,[Out] out IPropertyStorage propertyStorage); }
这是我的STGM枚举:
公共 枚举 STGM:诠释 { 读取= 0x00000000, 写= 0x00000001, READWRITE = 0x00000002, SHARE_DENY_NONE = 0x00000040, SHARE_DENY_READ = 0x00000030, SHARE_DENY_WRITE = 0x00000020, SHARE_EXCLUSIVE = 0x00000010, 优先权= 0x00040000, 创建= 0x00001000 转换= 0x00020000, FAILIFTHERE = 0x00000000, 直接= 0x00000000, 交易= 0x00010000, NOSCRATCH = 0x00100000, NOSNAPSHOT = 0x00200000, SIMPLE = 0x08000000, DIRECT_SWMR = 0x00400000, DELETEONRELEASE = 0x04000000 }
没有人有任何建议吗?
解决方案
Here is some code I''ve inherited in my job:
public string GetProperty(SummaryPropId summaryPropertID) { IPropertySetStorage pssSetStorage = null; Guid guidPropertySetStorage = new Guid("0000013A-0000-0000-C000-000000000046"); Guid guidSummaryProperties = new Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9"); IPropertyStorage psStorage = null; PropSpec[] pscSpecs = new PropSpec[1]; PropVariant[] pvValues = new PropVariant[1]; try { //Exit if file does not exist if (!fiSource.Exists) return null; // Open the file and its property set stream uint hresult = ole32.StgOpenStorageEx(fiSource.FullName, (int)(STGM.DIRECT_SWMR | STGM.READ | STGM.SHARE_DENY_NONE), (int)STGFMT.FILE, 0, System.IntPtr.Zero, System.IntPtr.Zero, ref guidPropertySetStorage, ref pssSetStorage); // Open the Summary Information property set int hresult2 = pssSetStorage.Open(ref guidSummaryProperties, (int)(STGM.DIRECT | STGM.SHARE_EXCLUSIVE), out psStorage); // Specify the property to be retrieved pscSpecs[0].ulKind = 1; pscSpecs[0].Name_Or_ID = new IntPtr((int)summaryPropertID); // Retrieve the value hresult2 = psStorage.ReadMultiple(1, pscSpecs, pvValues); // Release the Com objects Marshal.ReleaseComObject(psStorage); Marshal.ReleaseComObject(pssSetStorage); psStorage = null; pssSetStorage = null; } catch (Exception excException) { AuditMethodError(excException, System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " - " + excException.StackTrace); } finally { pssSetStorage = null; psStorage = null; pscSpecs = null; pvValues = null; } return Marshal.PtrToStringUni(pvValues[0].pointerValue); }
The problem is that when it gets to the line int hresult2 = pssSetStorage.Open(ref guidSummaryProperties, (int)(STGM.DIRECT | STGM.SHARE_EXCLUSIVE), out psStorage)
I''m receiving this exception:
excException = {"Object reference not set to an instance of an object."}
Here is my interface declaration:
[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); }
Here is my STGM enumeration:
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 }
Does anyone have any suggestions?
解决方案
这篇关于如何使用IPropertySetStorage获取文件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!