使用 C#,如何在 Windows Vista,7,2008 中复制符号链接
使用 C#,如何在 Windows Vista,7,2008 中复制符号链接。
当我使用 File.Copy 复制符号链接时,它的目标会被复制。
我希望模仿使用带有 /B 选项的 xcopy 时的行为。
使用 .NET 3.5 可以吗?
Using c#, how do I copy a symbolic link in Windows Vista,7,2008.
When I use File.Copy to copy a symlink, its target gets copied.
I wish to mimic the behavior one gets when you use xcopy with the /B option.
Is this possible using .NET 3.5?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 pinvoke 并调用 CopyFileEx。请注意您要查找的
COPY_FILE_COPY_SYMLINK
。You could make use of pinvoke and call CopyFileEx. Note the
COPY_FILE_COPY_SYMLINK
which is what you are looking for.我认识到这是一个相当古老的线程,但我刚刚完成了一些支持此功能的 C 代码工作(是的,确实)。发帖是因为我查遍了这方面的资料,而且不容易找到。
遗憾的是,
CopyFileEx
和COPY_FILE_SYMLINK
并不能解决问题。这只是在目标目录中创建一个具有正确名称的 0 大小文件,但没有符号链接。另一篇旧文章包含您可以尝试实现的解决方案: 如何复制目录符号链接作为目标的链接?。我确实认为
CopyFileEx
的问题是一个错误,在 Windows 10 中仍然存在。I recognize this is a fairly old thread, but I've just finished some work in C code (yes, really) that supports this. Posting because I searched all over for this information, and it was not easy to find.
Sadly
CopyFileEx
withCOPY_FILE_SYMLINK
does not do the trick. That simply creates a 0-size file in the target directory with the correct name, but no symbolic link. Another old article contains a solution you could try to implement: How to copy directory symbolic link as a link to the target?.I do think the problem with
CopyFileEx
is a bug, one that still exists in Windows 10.您可以使用 Win32 CopyFileEx 函数来执行此操作。这需要一些努力,但这里是整个 CopyFileEx 帮助程序类(C# 3.0 和 .NET 3.5 Client Profile 兼容并经过测试!)。您还可以将其重复用于您拥有的任何其他
CopyFileEx
任务:以下是一些示例代码,展示了如何使用它来复制符号链接(而不是它引用的文件):
You can use the Win32
CopyFileEx
function to do this. It took a bit of effort, but here is the wholeCopyFileEx
helper class (C# 3.0 and .NET 3.5 Client Profile compatible and tested!). You can also reuse it for any otherCopyFileEx
tasks that you have:Here is some sample code that shows how to use it to copy a symbolic link (and not the file it refers to):
.NET 框架中没有用于链接(硬链接、软链接或符号链接)的 API。
您必须调用
mklink.exe
使用Process.Start
并创建所需的链接,或者您可以寻找能够执行此类操作的第三方库。
There is no API for links (neither hardlinks, softlinks or symbolic links) in the .NET framework.
You must either call
mklink.exe
withProcess.Start
and create the link you want,or you have a look for a third party library which is able to do such things.