当前位置: 首页>>代码示例>>C#>>正文


C# IStorage.OpenStream方法代码示例

本文整理汇总了C#中IStorage.OpenStream方法的典型用法代码示例。如果您正苦于以下问题:C# IStorage.OpenStream方法的具体用法?C# IStorage.OpenStream怎么用?C# IStorage.OpenStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IStorage的用法示例。


在下文中一共展示了IStorage.OpenStream方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OpenStream

        private void OpenStream(IStorage parentStorage, ref FileAccess access)
        {
            ShellAPI.STGM grfmode = ShellAPI.STGM.SHARE_DENY_WRITE;

            switch (access)
            {
                case FileAccess.ReadWrite:
                    grfmode |= ShellAPI.STGM.READWRITE;
                    break;

                case FileAccess.Write:
                    grfmode |= ShellAPI.STGM.WRITE;
                    break;
            }

            if (parentStorage != null)
            {
                if (parentStorage.OpenStream(
                        shellItem.Text + (shellItem.IsLink ? ".lnk" : string.Empty),
                        IntPtr.Zero,
                        grfmode,
                        0,
                        out streamPtr) == ShellAPI.S_OK)
                {
                    stream = (IStream)Marshal.GetTypedObjectForIUnknown(streamPtr, typeof(IStream));
                }
                else if (access != FileAccess.Read)
                {
                    if (parentStorage.OpenStream(
                        shellItem.Text + (shellItem.IsLink ? ".lnk" : string.Empty),
                        IntPtr.Zero,
                        ShellAPI.STGM.SHARE_DENY_WRITE,
                        0,
                        out streamPtr) == ShellAPI.S_OK)
                    {
                        access = FileAccess.Read;
                        stream = (IStream)Marshal.GetTypedObjectForIUnknown(streamPtr, typeof(IStream));
                    }
                    else
                        throw new IOException(String.Format("Can't open stream: {0}", shellItem));
                }
                else
                    throw new IOException(String.Format("Can't open stream: {0}", shellItem));
            }
            else
            {
                access = FileAccess.Read;

                if (!ShellHelper.GetIStream(shellItem, out streamPtr, out stream))
                    throw new IOException(String.Format("Can't open stream: {0}", shellItem));
            }
        }
开发者ID:davidcon,项目名称:ScreenGrab,代码行数:52,代码来源:StreamStorage.cs

示例2: GetOleStream

        private byte[] GetOleStream(IStorage storage, comTypes.STATSTG statstg)
        {
            comTypes.IStream pIStream;
            storage.OpenStream(statstg.pwcsName,
               IntPtr.Zero,
               (uint)(STGM.READ | STGM.SHARE_EXCLUSIVE),
               0,
               out pIStream);

            byte[] data = new byte[statstg.cbSize];
            pIStream.Read(data, (int)statstg.cbSize, IntPtr.Zero);
            Marshal.ReleaseComObject(pIStream);

            return data;
        }
开发者ID:acinep,项目名称:epplus,代码行数:15,代码来源:CompoundDocument.cs

示例3: InitialThumbNameList

        private CatalogItem[] InitialThumbNameList(IStorage m_IStorage)
        {
            IStream catalogHeaderStream;
            m_IStorage.OpenStream(CATALOG_HEADER_NAME,
                IntPtr.Zero,
                (uint)(Win32.STGM.READWRITE | Win32.STGM.SHARE_EXCLUSIVE),
                0,
                out catalogHeaderStream);
            var header = new CatalogHeader();
            var size = Marshal.SizeOf(typeof(CatalogHeader));
            var buffer = new byte[size];
            var cbRead = Marshal.AllocCoTaskMem(4);

            try
            {
                catalogHeaderStream.Read(buffer, size, cbRead);
                if (size != Marshal.ReadInt32(cbRead))
                    throw new InvalidOperationException("在从Thumb.db读取CatalogHeader失败!");
            }
            finally
            {
                Marshal.FreeCoTaskMem(cbRead);
            }

            using (var reader = new BinaryReader(new MemoryStream(buffer)))
            {
                header.Reserved1 = reader.ReadInt16();
                header.Reserved2 = reader.ReadInt16();
                header.ThumbCount = reader.ReadInt32();
                header.ThumbHeight = reader.ReadInt32();
                header.ThumbWidth = reader.ReadInt32();
            }

            ThumbHeight = header.ThumbHeight;
            ThumbWidth = header.ThumbWidth;

            return ReadNameList(catalogHeaderStream, header);
        }
开发者ID:Prashant-Jonny,项目名称:phever,代码行数:38,代码来源:ThumbDbCom.cs

示例4: openStream

        /// <summary>
        /// Open a file stream, used by FileStreamEx
        /// </summary>
        internal static void openStream(IStorage parentStorage, string filename, ref FileMode mode, ref FileAccess access, out IntPtr streamPtr, out IStream stream)
        {
            ShellAPI.STGM grfmode = ShellAPI.STGM.SHARE_DENY_WRITE;

            switch (access)
            {
                case FileAccess.ReadWrite:
                    grfmode |= ShellAPI.STGM.READWRITE;
                    break;

                case FileAccess.Write:
                    grfmode |= ShellAPI.STGM.WRITE;
                    break;
            }

            switch (mode)
            {
                case FileMode.Create:
                    if (FileEx.Exists(filename))
                        grfmode |= ShellAPI.STGM.CREATE;
                    break;
                case FileMode.CreateNew:
                    grfmode |= ShellAPI.STGM.CREATE;
                    break;
            }

            if (parentStorage != null)
            {
                if (parentStorage.OpenStream(
                        filename,
                        IntPtr.Zero,
                        grfmode,
                        0,
                        out streamPtr) == ShellAPI.S_OK)
                {
                    stream = (IStream)Marshal.GetTypedObjectForIUnknown(streamPtr, typeof(IStream));
                }
                else if (access != FileAccess.Read)
                {
                    //Create file if not exists
                    if (parentStorage.CreateStream(
                        filename, ShellAPI.STGM.WRITE, 0, 0, out streamPtr) == ShellAPI.S_OK)
                    {
                        stream = (IStream)Marshal.GetTypedObjectForIUnknown(streamPtr, typeof(IStream));
                    }
                    else
                        throw new IOException(String.Format("Can't open stream: {0}", filename));
                }
                else
                    throw new IOException(String.Format("Can't open stream: {0}", filename));
            }
            else
                throw new IOException(String.Format("Can't open stream: {0}", filename));
        }
开发者ID:Choi-Insu,项目名称:arrengers,代码行数:57,代码来源:IOTools.cs

示例5: getFileStat

        /// <summary>
        /// return STATSTG info of a file.
        /// </summary>
        internal static bool getFileStat(IStorage parentStorage, string filename, out ShellAPI.STATSTG statstg)
        {
            IntPtr streamPtr = IntPtr.Zero;
            IStream stream = null;
            statstg = new ShellAPI.STATSTG();

            try
            {
                if (parentStorage.OpenStream(
                            filename,
                            IntPtr.Zero,
                            ShellAPI.STGM.READ,
                            0,
                            out streamPtr) == ShellAPI.S_OK)
                {
                    stream = (IStream)Marshal.GetTypedObjectForIUnknown(streamPtr, typeof(IStream));
                    stream.Stat(out statstg, ShellAPI.STATFLAG.DEFAULT);
                    return true;
                }
            }
            finally
            {
                if (stream != null)
                {
                    Marshal.ReleaseComObject(stream);
                    stream = null;
                }

                if (streamPtr != IntPtr.Zero)
                {
                    Marshal.Release(streamPtr);
                    streamPtr = IntPtr.Zero;
                }

            }
            return false;
        }
开发者ID:Choi-Insu,项目名称:arrengers,代码行数:40,代码来源:IOTools.cs

示例6: ProcessPackage

        static void ProcessPackage(IStorage pStg, string destinationFolder)
        {
            System.Runtime.InteropServices.ComTypes.IStream pStream;
            IEnumSTATSTG pEnumStatStg;
            uint numReturned;
            pStg.EnumElements(0, IntPtr.Zero, 0, out pEnumStatStg);
            System.Runtime.InteropServices.ComTypes.STATSTG[] ss = new System.Runtime.InteropServices.ComTypes.STATSTG[1];
            // Loop through the STATSTG structures in the storage.
            do
            {
                // Retrieve the STATSTG structure
                pEnumStatStg.Next(1, ss, out numReturned);
                if (numReturned != 0)
                {
                    //System.Runtime.InteropServices.ComTypes.STATSTG statstm;
                    byte[] bytT = new byte[4];
                    // Check if the pwcsName contains "Ole10Native" stream which contain the actual embedded object
                    if (ss[0].pwcsName.Contains("Ole10Native") == true)
                    {
                        // Get the stream objectOpen the stream
                        pStg.OpenStream(ss[0].pwcsName, IntPtr.Zero, (uint)STGM.READ | (uint)STGM.SHARE_EXCLUSIVE, 0, out pStream);
                        //pStream.Stat(out statstm, (int) STATFLAG.STATFLAG_DEFAULT);

                        IntPtr position = IntPtr.Zero;
                        // File name starts from 7th Byte.
                        // Position the cursor to the 7th Byte.
                        pStream.Seek(6, 0, position);

                        IntPtr ulRead = new IntPtr();
                        char[] filename = new char[260];
                        int i;

                        // Read the File name of the embedded object
                        for (i = 0; i < 260; i++)
                        {
                            pStream.Read(bytT, 1, ulRead);
                            pStream.Seek(0, 1, position);
                            filename[i] = (char)bytT[0];
                            if (bytT[0] == 0)
                            {
                                break;
                            }
                        }
                        string path = new string(filename, 0, i);

                        // Next part is the source path of the embedded object.
                        // Length is unknown. Hence, loop through each byte to read the 0 terminated string
                        // Read the source path.
                        for (i = 0; i < 260; i++)
                        {
                            pStream.Read(bytT, 1, ulRead);
                            pStream.Seek(0, 1, position);
                            filename[i] = (char)bytT[0];
                            if (bytT[0] == 0)
                            {
                                break;
                            }
                        }
                        // Source File path
                        string fullpath = new string(filename, 0, i);

                        // Unknown 4 bytes
                        pStream.Seek(4, 1, position);

                        // Next 4 byte gives the length of the temporary file path 
                        // (Office uses a temporary location to copy the files before inserting to the document)
                        // The length is in little endian format. Hence conversion is needed
                        pStream.Read(bytT, 4, ulRead);
                        ulong dwSize, dwTemp;
                        dwSize = 0;
                        dwTemp = (ulong)bytT[3];
                        dwSize += (ulong)(bytT[3] << 24);
                        dwSize += (ulong)(bytT[2] << 16);
                        dwSize += (ulong)(bytT[1] << 8);
                        dwSize += bytT[0];

                        // Skip the temporary file path
                        pStream.Seek((long)dwSize, 1, position);

                        // Next four bytes gives the size of the actual data in little endian format.
                        // Convert the format.
                        pStream.Read(bytT, 4, ulRead);
                        dwTemp = 0;
                        dwSize = 0;
                        dwTemp = (ulong)bytT[3];
                        dwSize += (ulong)(bytT[3] << 24);
                        dwSize += (ulong)(bytT[2] << 16);
                        dwSize += (ulong)(bytT[1] << 8);
                        dwSize += (ulong)bytT[0];

                        // Read the actual file content 
                        byte[] byData = new byte[dwSize];
                        pStream.Read(byData, (int)dwSize, ulRead);

                        // Create the file
                        var bWriter = new BinaryWriter(File.Open(Path.Combine(destinationFolder, path), FileMode.Create));
                        bWriter.Write(byData);
                        bWriter.Close();
                    }
                }
//.........这里部分代码省略.........
开发者ID:eHanlin,项目名称:Hanlin.Common,代码行数:101,代码来源:Ole10Native.cs

示例7: Load

		public int Load(IStorage pstg)
		{
			if (!(null == _document))
				throw new InvalidOperationException(nameof(_document) + " should be null");

			string documentName = null;
			Version altaxoVersion;
			ComDebug.ReportInfo("{0}.IPersistStorage.Load", this.GetType().Name);

			try
			{
				using (var stream = new ComStreamWrapper(pstg.OpenStream("AltaxoVersion", IntPtr.Zero, (int)(STGM.READ | STGM.SHARE_EXCLUSIVE), 0), true))
				{
					var bytes = new byte[stream.Length];
					stream.Read(bytes, 0, bytes.Length);
					var versionString = System.Text.Encoding.UTF8.GetString(bytes);
					altaxoVersion = Version.Parse(versionString);
				}
				ComDebug.ReportInfo("{0}.IPersistStorage.Load -> Version: {1}", this.GetType().Name, altaxoVersion);
			}
			catch (Exception ex)
			{
				ComDebug.ReportInfo("{0}.IPersistStorage.Load Failed to load stream AltaxoVersion, exception: {1}", this.GetType().Name, ex);
			}

			try
			{
				using (var stream = new ComStreamWrapper(pstg.OpenStream("AltaxoGraphName", IntPtr.Zero, (int)(STGM.READ | STGM.SHARE_EXCLUSIVE), 0), true))
				{
					var bytes = new byte[stream.Length];
					stream.Read(bytes, 0, bytes.Length);
					documentName = System.Text.Encoding.UTF8.GetString(bytes);
				}
				ComDebug.ReportInfo("{0}.IPersistStorage.Load -> Name of GraphDocument: {1}", this.GetType().Name, documentName);
			}
			catch (Exception ex)
			{
				ComDebug.ReportInfo("{0}.IPersistStorage.Load Failed to load stream AltaxoGraphName, exception: {1}", this.GetType().Name, ex);
			}

			try
			{
				using (var streamWrapper = new ComStreamWrapper(pstg.OpenStream("AltaxoProjectZip", IntPtr.Zero, (int)(STGM.READ | STGM.SHARE_EXCLUSIVE), 0), true))
				{
					_comManager.InvokeGuiThread(() =>
					{
						Current.ProjectService.CloseProject(true);
						Current.ProjectService.LoadProject(streamWrapper);
					});
				}
				ComDebug.ReportInfo("{0}.IPersistStorage.Load Project loaded", this.GetType().Name);
			}
			catch (Exception ex)
			{
				ComDebug.ReportInfo("{0}.IPersistStorage.Load Failed to load stream AltaxoProjectZip, exception: {1}", this.GetType().Name, ex);
			}

			Marshal.ReleaseComObject(pstg);

			Altaxo.Graph.GraphDocumentBase newDocument = null;

			if (null != documentName)
			{
				Altaxo.Graph.Gdi.GraphDocument newDocGdi;
				Altaxo.Graph.Graph3D.GraphDocument newDoc3D;

				if (Current.Project.GraphDocumentCollection.TryGetValue(documentName, out newDocGdi))
					newDocument = newDocGdi;
				else if (Current.Project.Graph3DDocumentCollection.TryGetValue(documentName, out newDoc3D))
					newDocument = newDoc3D;
			}

			if (null == newDocument)
			{
				if (null != Current.Project.GraphDocumentCollection.FirstOrDefault())
					newDocument = Current.Project.GraphDocumentCollection.First();
				else if (null != Current.Project.Graph3DDocumentCollection.FirstOrDefault())
					newDocument = Current.Project.Graph3DDocumentCollection.First();
			}

			if (null != newDocument)
			{
				Document = newDocument;
				_comManager.InvokeGuiThread(() => Current.ProjectService.ShowDocumentView(_document));
			}

			if (null == Document)
			{
				ComDebug.ReportError("{0}.IPersistStorage.Load Document is null, have to throw an exception now!!", this.GetType().Name);
				throw new InvalidOperationException();
			}

			_isDocumentDirty = false;
			return ComReturnValue.S_OK;
		}
开发者ID:Altaxo,项目名称:Altaxo,代码行数:95,代码来源:GraphDocumentEmbeddedComObject.cs


注:本文中的IStorage.OpenStream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。