Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have some <a href="http://troyparsons.com/blog/2012/03/symbolic-links-in-c-sharp/" rel="noreferrer">source code for symlinks posted on my blog</a> that will allow you to:</p> <ul> <li>create symlinks</li> <li>check whether a path is a symlink</li> <li>retrieve the target of a symlink</li> </ul> <p>It also contains NUnit test cases, that you may wish to extend.</p> <p>The meaty bit is:</p> <pre><code>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; } </code></pre> <p>That is:</p> <ul> <li>Open the file with <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx" rel="noreferrer"><code>CreateFile()</code></a></li> <li>Call <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363216%28v=vs.85%29.aspx" rel="noreferrer"><code>DeviceIoControl()</code></a> to get the reparse point data (NOTE: it could be a junction point!)</li> <li>Check out the <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff552012%28v=vs.85%29.aspx" rel="noreferrer">returned data structure</a> to inspect. The <a href="http://msdn.microsoft.com/en-us/library/dd541667%28v=prot.10%29.aspx" rel="noreferrer">reparse tag</a> will tell you if it is a junction point or symbolic link. This may be all you want to do.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload