.Net中的Junction Points(交接點(diǎn))操作
Junction Points是NTFS v5+的新特性,功能和我們所熟知的UNIX中的文件夾軟鏈接類似(與Windows中的文件夾快捷方式不同的是,它進(jìn)行了路徑重定向)。如在Vista中的C:\Documents and Settings 就是C:\Users的一個(gè)鏈接。然而,在Windows中并沒有提供相關(guān)命令,只能編程通過API來實(shí)現(xiàn)。
由于我對(duì)WinAPI編程并不熟,這里就不更多介紹了,更多信息請(qǐng)?jiān)L問原作者相關(guān)文章。這里僅轉(zhuǎn)一下相關(guān)函數(shù)用法:
創(chuàng)建Junction Point
// Creates a Junction Point at // C:\Foo\JunctionPoint that points to the directory C:\Bar. // Fails if there is already a file, // directory or Junction Point with the specified path. JunctionPoint.Create(@"C:\Foo\JunctionPoint", @"C:\Bar", false/*don't overwrite*/) // Creates a Junction Point at C:\Foo\JunctionPoint that points to // the directory C:\Bar. // Replaces an existing Junction Point if found at the specified path. JunctionPoint.Create(@"C:\Foo\JunctionPoint", @"C:\Bar", true/*overwrite*/)
注:不能對(duì)文件創(chuàng)建Junction Points.
刪除Junction Point
// Delete a Junction Point at C:\Foo\JunctionPoint if it exists. // Does nothing if there is no such Junction Point. // Fails if the specified path refers to an existing file or // directory rather than a Junction Point. JunctionPoint.Delete(@"C:\Foo\JunctionPoint")
判斷Junction Point是否存在
// Returns true if there is a Junction Point at C:\Foo\JunctionPoint. // Returns false if the specified path refers to an existing file // or directory rather than a Junction Point // or if it refers to the vacuum of space. bool exists = JunctionPoint.Exists(@"C:\Foo\JunctionPoint")
獲取Junction Point所指向的實(shí)際地址
// Create a Junction Point for demonstration purposes whose target is C:\Bar. JunctionPoint.Create(@"C:\Foo\JunctionPoint", @"C:\Bar", false) // Returns the full path of the target of the Junction Point at // C:\Foo\JunctionPoint. // Fails if the specified path does not refer to a Junction Point. string target = JunctionPoint.GetTarget(@"C:\Foo\JunctionPoint") // target will be C:\Bar
注:這個(gè)函數(shù)有問題,對(duì)系統(tǒng)的權(quán)限要求過高,如果對(duì)系統(tǒng)盤的一些文件夾鏈接訪問往往會(huì)報(bào)異常,我把它改了一下,新的代碼如下:
/// <summary>
/// Provides access to NTFS junction points in .Net.
/// </summary>
public static class JunctionPoint
{
/// <summary>
/// The file or directory is not a reparse point.
/// </summary>
private const int ERROR_NOT_A_REPARSE_POINT = 4390;
/// <summary>
/// The reparse point attribute cannot be set because it conflicts with an existing attribute.
/// </summary>
private const int ERROR_REPARSE_ATTRIBUTE_CONFLICT = 4391;
/// <summary>
/// The data present in the reparse point buffer is invalid.
/// </summary>
private const int ERROR_INVALID_REPARSE_DATA = 4392;
/// <summary>
/// The tag present in the reparse point buffer is invalid.
/// </summary>
private const int ERROR_REPARSE_TAG_INVALID = 4393;
/// <summary>
/// There is a mismatch between the tag specified in the request and the tag present in the reparse point.
/// </summary>
private const int ERROR_REPARSE_TAG_MISMATCH = 4394;
/// <summary>
/// Command to set the reparse point data block.
/// </summary>
private const int FSCTL_SET_REPARSE_POINT = 0x000900A4;
/// <summary>
/// Command to get the reparse point data block.
/// </summary>
private const int FSCTL_GET_REPARSE_POINT = 0x000900A8;
/// <summary>
/// Command to delete the reparse point data base.
/// </summary>
private const int FSCTL_DELETE_REPARSE_POINT = 0x000900AC;
/// <summary>
/// Reparse point tag used to identify mount points and junction points.
/// </summary>
private const uint IO_REPARSE_TAG_MOUNT_POINT = 0xA0000003;
/// <summary>
/// This prefix indicates to NTFS that the path is to be treated as a non-interpreted
/// path in the virtual file system.
/// </summary>
private const string NonInterpretedPathPrefix = @"\??\";
[Flags]
private enum EFileAccess : uint
{
GenericRead = 0x80000000,
GenericWrite = 0x40000000,
GenericExecute = 0x20000000,
GenericAll = 0x10000000,
GenericNone = 0x0,
}
[Flags]
private enum EFileShare : uint
{
None = 0x00000000,
Read = 0x00000001,
Write = 0x00000002,
Delete = 0x00000004,
}
private enum ECreationDisposition : uint
{
New = 1,
CreateAlways = 2,
OpenExisting = 3,
OpenAlways = 4,
TruncateExisting = 5,
}
[Flags]
private enum EFileAttributes : uint
{
Readonly = 0x00000001,
Hidden = 0x00000002,
System = 0x00000004,
Directory = 0x00000010,
Archive = 0x00000020,
Device = 0x00000040,
Normal = 0x00000080,
Temporary = 0x00000100,
SparseFile = 0x00000200,
ReparsePoint = 0x00000400,
Compressed = 0x00000800,
Offline = 0x00001000,
NotContentIndexed = 0x00002000,
Encrypted = 0x00004000,
Write_Through = 0x80000000,
Overlapped = 0x40000000,
NoBuffering = 0x20000000,
RandomAccess = 0x10000000,
SequentialScan = 0x08000000,
DeleteOnClose = 0x04000000,
BackupSemantics = 0x02000000,
PosixSemantics = 0x01000000,
OpenReparsePoint = 0x00200000,
OpenNoRecall = 0x00100000,
FirstPipeInstance = 0x00080000
}
[StructLayout(LayoutKind.Sequential)]
private struct REPARSE_DATA_BUFFER
{
/// <summary>
/// Reparse point tag. Must be a Microsoft reparse point tag.
/// </summary>
public uint ReparseTag;
/// <summary>
/// Size, in bytes, of the data after the Reserved member. This can be calculated by:
/// (4 * sizeof(ushort)) + SubstituteNameLength + PrintNameLength +
/// (namesAreNullTerminated ? 2 * sizeof(char) : 0);
/// </summary>
public ushort ReparseDataLength;
/// <summary>
/// Reserved; do not use.
/// </summary>
public ushort Reserved;
/// <summary>
/// Offset, in bytes, of the substitute name string in the PathBuffer array.
/// </summary>
public ushort SubstituteNameOffset;
/// <summary>
/// Length, in bytes, of the substitute name string. If this string is null-terminated,
/// SubstituteNameLength does not include space for the null character.
/// </summary>
public ushort SubstituteNameLength;
/// <summary>
/// Offset, in bytes, of the print name string in the PathBuffer array.
/// </summary>
public ushort PrintNameOffset;
/// <summary>
/// Length, in bytes, of the print name string. If this string is null-terminated,
/// PrintNameLength does not include space for the null character.
/// </summary>
public ushort PrintNameLength;
/// <summary>
/// A buffer containing the unicode-encoded path string. The path string contains
/// the substitute name string and print name string.
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x3FF0)]
public byte[] PathBuffer;
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode,
IntPtr InBuffer, int nInBufferSize,
IntPtr OutBuffer, int nOutBufferSize,
out int pBytesReturned, IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr CreateFile(
string lpFileName,
EFileAccess dwDesiredAccess,
EFileShare dwShareMode,
IntPtr lpSecurityAttributes,
ECreationDisposition dwCreationDisposition,
EFileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
/// <summary>
/// Creates a junction point from the specified directory to the specified target directory.
/// </summary>
/// <remarks>
/// Only works on NTFS.
/// </remarks>
/// <param name="junctionPoint">The junction point path</param>
/// <param name="targetDir">The target directory</param>
/// <param name="overwrite">If true overwrites an existing reparse point or empty directory</param>
/// <exception cref="IOException">Thrown when the junction point could not be created or when
/// an existing directory was found and <paramref name="overwrite" /> if false</exception>
public static void Create(string junctionPoint, string targetDir, bool overwrite)
{
targetDir = Path.GetFullPath(targetDir);
if (!Directory.Exists(targetDir))
throw new IOException("Target path does not exist or is not a directory.");
if (Directory.Exists(junctionPoint))
{
if (!overwrite)
throw new IOException("Directory already exists and overwrite parameter is false.");
}
else
{
Directory.CreateDirectory(junctionPoint);
}
using (SafeFileHandle handle = OpenReparsePoint(junctionPoint, EFileAccess.GenericWrite))
{
byte[] targetDirBytes = Encoding.Unicode.GetBytes(NonInterpretedPathPrefix + Path.GetFullPath(targetDir));
REPARSE_DATA_BUFFER reparseDataBuffer = new REPARSE_DATA_BUFFER();
reparseDataBuffer.ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
reparseDataBuffer.ReparseDataLength = (ushort)(targetDirBytes.Length + 12);
reparseDataBuffer.SubstituteNameOffset = 0;
reparseDataBuffer.SubstituteNameLength = (ushort)targetDirBytes.Length;
reparseDataBuffer.PrintNameOffset = (ushort)(targetDirBytes.Length + 2);
reparseDataBuffer.PrintNameLength = 0;
reparseDataBuffer.PathBuffer = new byte[0x3ff0];
Array.Copy(targetDirBytes, reparseDataBuffer.PathBuffer, targetDirBytes.Length);
int inBufferSize = Marshal.SizeOf(reparseDataBuffer);
IntPtr inBuffer = Marshal.AllocHGlobal(inBufferSize);
try
{
Marshal.StructureToPtr(reparseDataBuffer, inBuffer, false);
int bytesReturned;
bool result = DeviceIoControl(handle.DangerousGetHandle(), FSCTL_SET_REPARSE_POINT,
inBuffer, targetDirBytes.Length + 20, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);
if (!result)
ThrowLastWin32Error("Unable to create junction point.");
}
finally
{
Marshal.FreeHGlobal(inBuffer);
}
}
}
/// <summary>
/// Deletes a junction point at the specified source directory along with the directory itself.
/// Does nothing if the junction point does not exist.
/// </summary>
/// <remarks>
/// Only works on NTFS.
/// </remarks>
/// <param name="junctionPoint">The junction point path</param>
public static void Delete(string junctionPoint)
{
if (!Directory.Exists(junctionPoint))
{
if (File.Exists(junctionPoint))
throw new IOException("Path is not a junction point.");
return;
}
using (SafeFileHandle handle = OpenReparsePoint(junctionPoint, EFileAccess.GenericWrite))
{
REPARSE_DATA_BUFFER reparseDataBuffer = new REPARSE_DATA_BUFFER();
reparseDataBuffer.ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
reparseDataBuffer.ReparseDataLength = 0;
reparseDataBuffer.PathBuffer = new byte[0x3ff0];
int inBufferSize = Marshal.SizeOf(reparseDataBuffer);
IntPtr inBuffer = Marshal.AllocHGlobal(inBufferSize);
try
{
Marshal.StructureToPtr(reparseDataBuffer, inBuffer, false);
int bytesReturned;
bool result = DeviceIoControl(handle.DangerousGetHandle(), FSCTL_DELETE_REPARSE_POINT,
inBuffer, 8, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);
if (!result)
ThrowLastWin32Error("Unable to delete junction point.");
}
finally
{
Marshal.FreeHGlobal(inBuffer);
}
try
{
Directory.Delete(junctionPoint);
}
catch (IOException ex)
{
throw new IOException("Unable to delete junction point.", ex);
}
}
}
/// <summary>
/// Determines whether the specified path exists and refers to a junction point.
/// </summary>
/// <param name="path">The junction point path</param>
/// <returns>True if the specified path represents a junction point</returns>
/// <exception cref="IOException">Thrown if the specified path is invalid
/// or some other error occurs</exception>
public static bool Exists(string path)
{
if (!Directory.Exists(path))
return false;
using (SafeFileHandle handle = OpenReparsePoint(path, EFileAccess.GenericRead))
{
string target = InternalGetTarget(handle);
return target != null;
}
}
/// <summary>
/// Gets the target of the specified junction point.
/// </summary>
/// <remarks>
/// Only works on NTFS.
/// </remarks>
/// <param name="junctionPoint">The junction point path</param>
/// <returns>The target of the junction point</returns>
/// <exception cref="IOException">Thrown when the specified path does not
/// exist, is invalid, is not a junction point, or some other error occurs</exception>
public static string GetTarget(string junctionPoint)
{
using (SafeFileHandle handle = OpenReparsePoint(junctionPoint, EFileAccess.GenericNone))
{
string target = InternalGetTarget(handle);
if (target == null)
throw new IOException("Path is not a junction point.");
return target;
}
}
private static string InternalGetTarget(SafeFileHandle handle)
{
int outBufferSize = Marshal.SizeOf(typeof(REPARSE_DATA_BUFFER));
IntPtr outBuffer = Marshal.AllocHGlobal(outBufferSize);
try
{
int bytesReturned;
bool result = DeviceIoControl(handle.DangerousGetHandle(), FSCTL_GET_REPARSE_POINT,
IntPtr.Zero, 0, outBuffer, outBufferSize, out bytesReturned, IntPtr.Zero);
if (!result)
{
int error = Marshal.GetLastWin32Error();
if (error == ERROR_NOT_A_REPARSE_POINT)
return null;
ThrowLastWin32Error("Unable to get information about junction point.");
}
REPARSE_DATA_BUFFER reparseDataBuffer = (REPARSE_DATA_BUFFER)
Marshal.PtrToStructure(outBuffer, typeof(REPARSE_DATA_BUFFER));
if (reparseDataBuffer.ReparseTag != IO_REPARSE_TAG_MOUNT_POINT)
return null;
string targetDir = Encoding.Unicode.GetString(reparseDataBuffer.PathBuffer,
reparseDataBuffer.SubstituteNameOffset, reparseDataBuffer.SubstituteNameLength);
if (targetDir.StartsWith(NonInterpretedPathPrefix))
targetDir = targetDir.Substring(NonInterpretedPathPrefix.Length);
return targetDir;
}
finally
{
Marshal.FreeHGlobal(outBuffer);
}
}
private static SafeFileHandle OpenReparsePoint(string reparsePoint, EFileAccess accessMode)
{
SafeFileHandle reparsePointHandle = new SafeFileHandle(CreateFile(reparsePoint, accessMode,
EFileShare.Read | EFileShare.Write | EFileShare.Delete,
IntPtr.Zero, ECreationDisposition.OpenExisting,
EFileAttributes.BackupSemantics | EFileAttributes.OpenReparsePoint, IntPtr.Zero), true);
if (Marshal.GetLastWin32Error() != 0)
ThrowLastWin32Error("Unable to open reparse point.");
return reparsePointHandle;
}
private static void ThrowLastWin32Error(string message)
{
throw new IOException(message, Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()));
}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于asp.net button按鈕的OnClick和OnClientClick事件
OnClick是button的服務(wù)器端事件 OnClientClick是button的客戶端事件2009-05-05
在Asp.Net Core中使用ModelConvention實(shí)現(xiàn)全局過濾器隔離
這篇文章主要介紹了在Asp.Net Core中使用ModelConvention實(shí)現(xiàn)全局過濾器隔離,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
DataList 中動(dòng)態(tài)綁定服務(wù)器子控件的代碼
DataList 中動(dòng)態(tài)綁定服務(wù)器子控件的代碼...2007-09-09
把jQuery的each(callback)方法移植到c#中
jQuery中使用each(callback)方法可以很方便的遍歷集合,如2008-03-03
WPF關(guān)鍵幀動(dòng)畫介紹與實(shí)現(xiàn)
這篇文章介紹了WPF關(guān)鍵幀動(dòng)畫與實(shí)現(xiàn)方式,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
asp.net 臨時(shí)數(shù)據(jù)保存實(shí)現(xiàn)代碼
在一個(gè)程序運(yùn)行的時(shí)候往往我們會(huì)回到上一個(gè)頁(yè)面,或者我們會(huì)需要當(dāng)時(shí)留在這個(gè)頁(yè)面的臨時(shí)數(shù)據(jù),例如,我們用百度搜索“腳本”之后我們會(huì)看到很多那啥我就不再說了??!然后我們返回本來頁(yè)面在文本框內(nèi)會(huì)有“腳本”的字樣,我們一下代碼段就是實(shí)現(xiàn)這個(gè)功能2012-04-04
ASP.NET MVC+EF框架+EasyUI實(shí)現(xiàn)權(quán)限管系列
在學(xué)習(xí)MVC之前,我們有必要知道這些知識(shí)點(diǎn)(自動(dòng)屬性,隱式類型var,對(duì)象初始化器和集合初始化器,匿名類,擴(kuò)展方法,Lambda表達(dá)式),如果你還不知道的話就請(qǐng)看我下面的簡(jiǎn)單的介紹,看下面我建立的項(xiàng)目的初步圖像,然后下篇我們開始簡(jiǎn)單的介紹。2014-11-11
asp.net web頁(yè)面元素的多語(yǔ)言化(多國(guó)語(yǔ)化)實(shí)現(xiàn)分享
開發(fā)的一些系統(tǒng),經(jīng)常要求支持多語(yǔ)言(例如日文,英文等),接下來介紹如何實(shí)現(xiàn)asp.net開發(fā)中web頁(yè)面實(shí)現(xiàn)頁(yè)面元素的多語(yǔ)言化(多國(guó)語(yǔ)化)感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)有所幫助2013-02-02
.net平臺(tái)的rabbitmq使用封裝demo詳解
這篇文章主要針對(duì)rabbitmq學(xué)習(xí)后封裝RabbitMQ.Client的一個(gè)分享,文章最后,我會(huì)把封裝組件和demo奉上,對(duì).net平臺(tái)的rabbitmq使用封裝相關(guān)知識(shí)感興趣的朋友一起看看吧2021-09-09

