CreateFile Fails for SymLink to File in Parent Folder

落花浮王杯 提交于 2019-12-11 05:46:35

问题


In reference to this question:

File.Copy() and Symbolic Links

I find that the line

SafeFileHandle fileHandle = 
    CreateFile(symlink.FullName, 0, 2, 
               IntPtr.Zero, 
               CREATION_DISPOSITION_OPEN_EXISTING, 
               FILE_FLAG_BACKUP_SEMANTICS, 
               IntPtr.Zero);

is failing with the error

The system cannot find the file specified

for the following very specific case:

Original file is in a parent directory

C:\Temp\SymlinkUnitTest\Original.txt [real file]

Symbolic link is in a real subdirectory

C:\Temp\SymlinkUnitTest\Work\Symlink.txt [sym link to above file]

It seems to work in many other cases (all unit tests in the referenced post pass).

Is there something special about this particular case?

For reference, here's the unit test that is failing:

[TestMethod]
public void FileSymlinkWork()
{
    string file = @"C:\Temp\SymlinkUnitTest\Work\Symlink.txt";
    Assert.IsTrue(File.Exists(file)); // Succeeds 

    // Following line throws Exception:
    string actual = new FileInfo(file).GetSymbolicLinkTarget();
    Assert.IsTrue(actual.EndsWith(@"SymlinkUnitTest\Original.txt"));
}

来源:https://stackoverflow.com/questions/13831759/createfile-fails-for-symlink-to-file-in-parent-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!