本文整理汇总了C#中SafeFileHandle.DangerousRelease方法的典型用法代码示例。如果您正苦于以下问题:C# SafeFileHandle.DangerousRelease方法的具体用法?C# SafeFileHandle.DangerousRelease怎么用?C# SafeFileHandle.DangerousRelease使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SafeFileHandle
的用法示例。
在下文中一共展示了SafeFileHandle.DangerousRelease方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Poll
/// <summary>
/// Polls a File Descriptor for the passed in flags.
/// </summary>
/// <param name="fd">The descriptor to poll</param>
/// <param name="events">The events to poll for</param>
/// <param name="timeout">The amount of time to wait; -1 for infinite, 0 for immediate return, and a positive number is the number of milliseconds</param>
/// <param name="triggered">The events that were returned by the poll call. May be PollEvents.POLLNONE in the case of a timeout.</param>
/// <returns>An error or Error.SUCCESS.</returns>
internal static unsafe Error Poll(SafeFileHandle fd, PollEvents events, int timeout, out PollEvents triggered)
{
bool gotRef = false;
try
{
fd.DangerousAddRef(ref gotRef);
var pollEvent = new PollEvent
{
FileDescriptor = fd.DangerousGetHandle().ToInt32(),
Events = events,
};
uint unused;
Error err = Poll(&pollEvent, 1, timeout, &unused);
triggered = pollEvent.TriggeredEvents;
return err;
}
finally
{
if (gotRef)
{
fd.DangerousRelease();
}
}
}
示例2: WinInternalGetTarget
private static string WinInternalGetTarget(SafeFileHandle handle)
{
int outBufferSize = ClrFacade.SizeOf<REPARSE_DATA_BUFFER_SYMBOLICLINK>();
IntPtr outBuffer = Marshal.AllocHGlobal(outBufferSize);
bool success = false;
try
{
int bytesReturned;
//OACR warning 62001 about using DeviceIOControl has been disabled.
// According to MSDN guidance DangerousAddRef() and DangerousRelease() have been used.
handle.DangerousAddRef(ref success);
bool result = DeviceIoControl(handle.DangerousGetHandle(), FSCTL_GET_REPARSE_POINT,
IntPtr.Zero, 0, outBuffer, outBufferSize, out bytesReturned, IntPtr.Zero);
if (!result)
{
int lastError = Marshal.GetLastWin32Error();
if (lastError == ERROR_NOT_A_REPARSE_POINT)
return null;
throw new Win32Exception(lastError);
}
//Unmarshal to symbolic link to look for tags.
REPARSE_DATA_BUFFER_SYMBOLICLINK reparseDataBuffer = ClrFacade.PtrToStructure<REPARSE_DATA_BUFFER_SYMBOLICLINK>(outBuffer);
if (reparseDataBuffer.ReparseTag != IO_REPARSE_TAG_SYMLINK && reparseDataBuffer.ReparseTag != IO_REPARSE_TAG_MOUNT_POINT)
return null;
string targetDir = null;
if (reparseDataBuffer.ReparseTag == IO_REPARSE_TAG_SYMLINK)
{
targetDir = Encoding.Unicode.GetString(reparseDataBuffer.PathBuffer, reparseDataBuffer.SubstituteNameOffset, reparseDataBuffer.SubstituteNameLength);
}
if (reparseDataBuffer.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
{
//Since this is a junction we need to unmarshal to the correct structure.
REPARSE_DATA_BUFFER_MOUNTPOINT reparseDataBufferMountPoint = ClrFacade.PtrToStructure<REPARSE_DATA_BUFFER_MOUNTPOINT>(outBuffer);
targetDir = Encoding.Unicode.GetString(reparseDataBufferMountPoint.PathBuffer, reparseDataBufferMountPoint.SubstituteNameOffset, reparseDataBufferMountPoint.SubstituteNameLength);
}
if (targetDir.StartsWith(NonInterpretedPathPrefix, StringComparison.OrdinalIgnoreCase))
targetDir = targetDir.Substring(NonInterpretedPathPrefix.Length);
return targetDir;
}
finally
{
if (success)
{
handle.DangerousRelease();
}
Marshal.FreeHGlobal(outBuffer);
}
}
示例3: WinIsHardLink
internal static bool WinIsHardLink(FileSystemInfo fileInfo)
{
bool isHardLink = false;
// only check for hard link if the item is not directory
if (!((fileInfo.Attributes & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory))
{
IntPtr nativeHandle = InternalSymbolicLinkLinkCodeMethods.CreateFile(
fileInfo.FullName,
InternalSymbolicLinkLinkCodeMethods.FileDesiredAccess.GenericRead,
InternalSymbolicLinkLinkCodeMethods.FileShareMode.Read,
IntPtr.Zero,
InternalSymbolicLinkLinkCodeMethods.FileCreationDisposition.OpenExisting,
InternalSymbolicLinkLinkCodeMethods.FileAttributes.Normal,
IntPtr.Zero);
using (SafeFileHandle handle = new SafeFileHandle(nativeHandle, true))
{
bool success = false;
try
{
handle.DangerousAddRef(ref success);
IntPtr dangerousHandle = handle.DangerousGetHandle();
isHardLink = InternalSymbolicLinkLinkCodeMethods.IsHardLink(ref dangerousHandle);
}
finally
{
if (success)
handle.DangerousRelease();
}
}
}
return isHardLink;
}