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


C# FileStream.SafeDispose方法代码示例

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


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

示例1: IncreaseSize

        public Stream IncreaseSize(int value, out long offset)
        {
            lock (_lock)
            {
                while (Interlocked.Read(ref _counter) != 0)
                {
                    if (!Monitor.Wait(_lock, 10000, true))
                        throw new NotSupportedException();
                }

                FileStream file = new FileStream(_filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                try
                {
                    offset = MathEx.RoundUp(file.Length, 0x800);
                    file.SetLength(offset + value);
                    _mmf = MemoryMappedFile.CreateFromFile(file, null, 0, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.Inheritable, false);
                }
                catch
                {
                    file.SafeDispose();
                    throw;
                }

                Interlocked.Increment(ref _counter);
                DisposableStream result = new DisposableStream(_mmf.CreateViewStream(offset, value, MemoryMappedFileAccess.ReadWrite));
                result.AfterDispose.Add(new DisposableAction(Free));
                return result;
            }
        }
开发者ID:kidaa,项目名称:Pulse,代码行数:29,代码来源:SharedMemoryMappedFile.cs

示例2: Unzip

        public static void Unzip(string targetDirectoryPath, ZipArchive archive)
        {
            foreach (var zipEntry in archive.Entries)
            {
                var outFilePath = Path.Combine(targetDirectoryPath, zipEntry.FullName);
                var outFileInfo = new FileInfo(outFilePath);
                if (!outFileInfo.Directory.Exists)
                {
                    outFileInfo.Directory.Create();
                }

                Stream outFileStream = null;
                Stream inputStream = null;
                try
                {
                    outFileStream = new FileStream(
                        outFilePath,
                        FileMode.CreateNew,
                        FileAccess.Write,
                        FileShare.None);

                    inputStream = zipEntry.Open();

                    inputStream.CopyTo(outFileStream);
                }
                finally
                {
                    outFileStream.SafeDispose();
                    inputStream.SafeDispose();
                }
            }
        }
开发者ID:GalenHealthcare,项目名称:Galen.Ef.Deployer,代码行数:32,代码来源:ZipUtility.cs

示例3: Verify

        private static void Verify(string filePath, byte[] expectedHash, string hashName)
        {
            HashAlgorithm hashAlgorithm = null;
            Stream inputStream = null;
            try
            {
                hashAlgorithm = HashAlgorithm.Create(hashName);
                inputStream = new FileStream(
                    filePath,
                    FileMode.Open,
                    FileAccess.Read,
                    FileShare.None);

                var readBuffer = new byte[StreamReadBufferSize];
                int bytesRead;
                while ((bytesRead = inputStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                {
                    hashAlgorithm.TransformBlock(readBuffer, 0, bytesRead, readBuffer, 0);
                }
                hashAlgorithm.TransformFinalBlock(readBuffer, 0, 0);

                var isHashMatches = hashAlgorithm.Hash.SequenceEqual(expectedHash);
                if (!isHashMatches)
                {
                    var expectedHashString = GetHexString(expectedHash);
                    var actualHashString = GetHexString(hashAlgorithm.Hash);
                    throw new Exception(
                        $"Checksum for {filePath} does not match.  Expected: {expectedHashString}, Actual: {actualHashString}");
                }
            }
            finally
            {
                inputStream.SafeDispose();
                hashAlgorithm.SafeDispose();
            }
        }
开发者ID:GalenHealthcare,项目名称:Galen.Ef.Deployer,代码行数:36,代码来源:HashUtility.cs


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