Hook into the Windows File Copy API from C#

假如想象 提交于 2019-12-04 17:18:59

I wish to actually change the copy feature of Windows to be more rigid

You shouldn't do that in managed code, because of the same reasons you should not write managed shell extensions.

Update: As others have stated, why not just use System.IO.File.Copy(...)? It calls this same underlying API. As Michael G points out, perhaps you intend to call the the FileCopyEx API that allows you to hook progress-indication callbacks(???) That's really the only reason to P/Invoke file-copy stuff in .NET. Details on how to implement FileCopyEx that can be found here: http://pinvoke.net/default.aspx/kernel32/CopyFileEx.html

Original answer: (which you really shouldn't use...)

Code snippet removed because you really shouldn't use it... If you're hell-bent on making busted-code, you can find out how to use it at: Found at http://pinvoke.net/default.aspx/kernel32/CopyFile.html

The other benefit of using unmanaged Copy File API is the ability to have a progress callback.

You can see an example of how to do so here.

Note: as stated in other answers, I would use the managed version of File.Copy as it's safer, and can usually do everything you require.

You can do so by calling System.IO.File.Copy. Its internal implementation already uses the Windows API.

Edit: File.Copy also handles permissions correctly and has the benefit of throwing an exception with meaningful data if something fails, so you don't have to manually check and analyze the return status.

You can use Deviare API Hook that lets you intercept any API from .NET and read parameters using VARIANT types. There is a full example very easy to follow in C#.

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