|
7 | 7 | namespace PhpVersionSwitcher |
8 | 8 | { |
9 | 9 | internal static class Symlinks |
10 | | - { |
11 | | - private const int CreationDispositionOpenExisting = 3; |
12 | | - private const int FileFlagBackupSemantics = 0x02000000; |
13 | | - private const int SymbolicLinkFlagDirectory = 1; |
14 | | - |
15 | | - // http://msdn.microsoft.com/en-us/library/aa364962(v=vs.85).aspx |
16 | | - [DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)] |
17 | | - private static extern int GetFinalPathNameByHandle(IntPtr handle, [In, Out] StringBuilder path, int bufLen, int flags); |
18 | | - |
19 | | - // http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx |
20 | | - [DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)] |
21 | | - private static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr securityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile); |
22 | | - |
23 | | - // http://msdn.microsoft.com/en-us/library/aa363866(v=vs.85).aspx |
24 | | - [DllImport("kernel32.dll", EntryPoint = "CreateSymbolicLinkW", CharSet = CharSet.Unicode, SetLastError = true)] |
25 | | - private static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags); |
26 | | - |
27 | | - public static string GetTarget(string symlink) |
28 | | - { |
29 | | - SafeFileHandle fileHandle = CreateFile(symlink, 0, 2, IntPtr.Zero, CreationDispositionOpenExisting, FileFlagBackupSemantics, IntPtr.Zero); |
30 | | - if (fileHandle.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); |
31 | | - |
32 | | - var path = new StringBuilder(512); |
33 | | - int size = GetFinalPathNameByHandle(fileHandle.DangerousGetHandle(), path, path.Capacity, 0); |
34 | | - if (size < 0) throw new Win32Exception(Marshal.GetLastWin32Error()); |
35 | | - |
36 | | - // The remarks section of GetFinalPathNameByHandle mentions the return being prefixed with "\\?\" |
37 | | - // More information about "\\?\" here -> http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx |
38 | | - var pathStr = path.ToString(); |
39 | | - if (pathStr.StartsWith(@"\\?\")) pathStr = pathStr.Substring(4); |
40 | | - return pathStr; |
41 | | - } |
42 | | - |
43 | | - public static bool CreateDir(string symlink, string target) |
44 | | - { |
45 | | - return CreateSymbolicLink(symlink, target, SymbolicLinkFlagDirectory); |
46 | | - } |
47 | | - } |
| 10 | + { |
| 11 | + private const int CreationDispositionOpenExisting = 3; |
| 12 | + private const int FileFlagBackupSemantics = 0x02000000; |
| 13 | + private const int SymbolicLinkFlagDirectory = 1; |
| 14 | + |
| 15 | + // http://msdn.microsoft.com/en-us/library/aa364962(v=vs.85).aspx |
| 16 | + [DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)] |
| 17 | + private static extern int GetFinalPathNameByHandle(IntPtr handle, [In, Out] StringBuilder path, int bufLen, int flags); |
| 18 | + |
| 19 | + // http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx |
| 20 | + [DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)] |
| 21 | + private static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr securityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile); |
| 22 | + |
| 23 | + // http://msdn.microsoft.com/en-us/library/aa363866(v=vs.85).aspx |
| 24 | + [DllImport("kernel32.dll", EntryPoint = "CreateSymbolicLinkW", CharSet = CharSet.Unicode, SetLastError = true)] |
| 25 | + private static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags); |
| 26 | + |
| 27 | + public static string GetTarget(string symlink) |
| 28 | + { |
| 29 | + SafeFileHandle fileHandle = CreateFile(symlink, 0, 2, IntPtr.Zero, CreationDispositionOpenExisting, FileFlagBackupSemantics, IntPtr.Zero); |
| 30 | + if (fileHandle.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 31 | + |
| 32 | + var path = new StringBuilder(512); |
| 33 | + int size = GetFinalPathNameByHandle(fileHandle.DangerousGetHandle(), path, path.Capacity, 0); |
| 34 | + if (size < 0) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 35 | + |
| 36 | + // The remarks section of GetFinalPathNameByHandle mentions the return being prefixed with "\\?\" |
| 37 | + // More information about "\\?\" here -> http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx |
| 38 | + var pathStr = path.ToString(); |
| 39 | + if (pathStr.StartsWith(@"\\?\")) pathStr = pathStr.Substring(4); |
| 40 | + return pathStr; |
| 41 | + } |
| 42 | + |
| 43 | + public static bool CreateDir(string symlink, string target) |
| 44 | + { |
| 45 | + return CreateSymbolicLink(symlink, target, SymbolicLinkFlagDirectory); |
| 46 | + } |
| 47 | + } |
48 | 48 | } |
0 commit comments