本文整理汇总了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;
}
}
示例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();
}
}
}
示例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();
}
}