[英]File.Copy() and Symbolic Links
I want to copy a file to a new file name. 我想将文件复制到新文件名。 Sometimes the source file might be a symbolic (file) link, created with
有时源文件可能是使用创建的符号(文件)链接
mklink C:\MyPath\ThisIsASymbolicLink.xml C:\MyPath\ThisIsTheOriginal.xml
I'm using this code: 我正在使用此代码:
string from = @"C:\MyPath\ThisIsASymbolicLink.xml";
string to = @"C:\MyPath\WantCopyOfOriginalFileHere.xml";
File.Copy(from, to, true);
However, I receive an IOException 但是,我收到IOException
The name of the file cannot be resolved by the system.
系统无法解析文件的名称。
when the from
file is really a symbolic link. 当
from
文件真的是一个符号链接。
How can I code for the cases where the source file might be a real file, or a symbolic link to a file? 如何编写源文件可能是真实文件或文件的符号链接的情况?
Expanding on this blog post , I created extension methods that take a DirectoryInfo or FileInfo that can refer to either an original or a symbolic link, and return a string indicating the fully qualified path name of the original file. 在这篇博文上进行了扩展,我创建了一个扩展方法,它采用DirectoryInfo或FileInfo,可以引用原始链接或符号链接,并返回一个字符串,指示原始文件的完全限定路径名。
App Code 应用代码
The application code is modified as follows: 应用程序代码修改如下:
// Works whether or not file is a symbolic link
string from =
new FileInfo(@"C:\MyPath\ThisIsASymbolicLink.xml").GetSymbolicLinkTarget();
Extension Method Code 扩展方法代码
private const int FILE_SHARE_READ = 1;
private const int FILE_SHARE_WRITE = 2;
private const int CREATION_DISPOSITION_OPEN_EXISTING = 3;
private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
// http://msdn.microsoft.com/en-us/library/aa364962%28VS.85%29.aspx
[DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetFinalPathNameByHandle(IntPtr handle, [In, Out] StringBuilder path, int bufLen, int flags);
// http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
[DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode,
IntPtr SecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
public static string GetSymbolicLinkTarget(this FileSystemInfo symlink)
{
using (SafeFileHandle fileHandle = CreateFile(symlink.FullName, 0, 2, System.IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, System.IntPtr.Zero))
{
if (fileHandle.IsInvalid)
throw new Win32Exception(Marshal.GetLastWin32Error());
StringBuilder path = new StringBuilder(512);
int size = GetFinalPathNameByHandle(fileHandle.DangerousGetHandle(), path, path.Capacity, 0);
if (size < 0)
throw new Win32Exception(Marshal.GetLastWin32Error());
// The remarks section of GetFinalPathNameByHandle mentions the return being prefixed with "\\?\"
// More information about "\\?\" here -> http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx
if (path[0] == '\\' && path[1] == '\\' && path[2] == '?' && path[3] == '\\')
return path.ToString().Substring(4);
else
return path.ToString();
}
}
Unit Tests 单元测试
[TestClass]
public class SymlinkTest
{
[TestInitialize]
public void SetupFiles()
{
if (!File.Exists(@"C:\Temp\SymlinkUnitTest\Original.txt")) throw new Exception("Run Symlinksetup.bat as Admin to create test data.");
}
[TestMethod]
public void OrdinaryFile()
{
string file = @"C:\Temp\SymlinkUnitTest\Original.txt";
string actual = new FileInfo(file).GetSymbolicLinkTarget();
Assert.IsTrue(actual.EndsWith(@"SymlinkUnitTest\Original.txt"));
}
[TestMethod]
public void FileSymlink()
{
string file = @"C:\Temp\SymlinkUnitTest\Symlink.txt";
string actual = new FileInfo(file).GetSymbolicLinkTarget();
Assert.IsTrue(actual.EndsWith(@"SymlinkUnitTest\Original.txt"));
}
[TestMethod]
public void OrdinaryDirectory()
{
string dir = @"C:\Temp\SymlinkUnitTest";
string actual = new DirectoryInfo(dir).GetSymbolicLinkTarget();
Assert.IsTrue(actual.EndsWith(@"SymlinkUnitTest"));
}
[TestMethod]
public void DirectorySymlink()
{
string dir = @"C:\Temp\SymlinkUnitTest";
string actual = new DirectoryInfo(dir).GetSymbolicLinkTarget();
Assert.IsTrue(actual.EndsWith(@"SymlinkUnitTest"));
}
}
Batch File to Create Unit Test Data 批处理文件以创建单元测试数据
Must be run as Administrator... a requirement of mklink. 必须以管理员身份运行... mklink的要求。
@Echo Off
Echo Must be run as Administrator (due to mklink)
mkdir C:\Temp
mkdir C:\Temp\SymlinkUnitTest
c:
cd C:\Temp\SymlinkUnitTest
echo Original File>Original.txt
mklink Symlink.txt Original.txt
mklink /D C:\Temp\SymlinkUnitTest\SymDir C:\Temp\SymlinkUnitTest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.