ho qualche source code for symlinks posted on my blog che vi permetterà di:
- creare link simbolici
- verifica se un percorso è un collegamento simbolico
- recuperare la destinazione di un link simbolico
contiene anche Casi di test NUnit, che potresti voler estendere.
Il bit carnosa è:
private static SafeFileHandle getFileHandle(string path)
{
return CreateFile(path, genericReadAccess, shareModeAll, IntPtr.Zero, openExisting,
fileFlagsForOpenReparsePointAndBackupSemantics, IntPtr.Zero);
}
public static string GetTarget(string path)
{
SymbolicLinkReparseData reparseDataBuffer;
using (SafeFileHandle fileHandle = getFileHandle(path))
{
if (fileHandle.IsInvalid)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
int outBufferSize = Marshal.SizeOf(typeof(SymbolicLinkReparseData));
IntPtr outBuffer = IntPtr.Zero;
try
{
outBuffer = Marshal.AllocHGlobal(outBufferSize);
int bytesReturned;
bool success = DeviceIoControl(
fileHandle.DangerousGetHandle(), ioctlCommandGetReparsePoint, IntPtr.Zero, 0,
outBuffer, outBufferSize, out bytesReturned, IntPtr.Zero);
fileHandle.Close();
if (!success)
{
if (((uint)Marshal.GetHRForLastWin32Error()) == pathNotAReparsePointError)
{
return null;
}
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
reparseDataBuffer = (SymbolicLinkReparseData)Marshal.PtrToStructure(
outBuffer, typeof(SymbolicLinkReparseData));
}
finally
{
Marshal.FreeHGlobal(outBuffer);
}
}
if (reparseDataBuffer.ReparseTag != symLinkTag)
{
return null;
}
string target = Encoding.Unicode.GetString(reparseDataBuffer.PathBuffer,
reparseDataBuffer.PrintNameOffset, reparseDataBuffer.PrintNameLength);
return target;
}
Cioè:
- Aprire il file con
CreateFile()
- chiamata
DeviceIoControl()
per ottenere i dati dei punti di analisi (NOTA: potrebbe essere un punto di giunzione !)
- Controllare il returned data structure da ispezionare. Il reparse tag ti dirà se si tratta di un punto di giunzione o di un collegamento simbolico. Questo potrebbe essere tutto quello che vuoi fare.
fonte
2012-03-27 03:02:49
Se è possibile, si può copiare il codice completo qui incollato, è necessario andare in blocco, altrimenti non verrà compilato? – TarmoPikaro
Questo codice è ora un [pacchetto NuGet] (https://www.nuget.org/packages/SymbolicLinkSupport/) e [è su GitHub] (https://github.com/michaelmelancon/symboliclinksupport). –