用.NET 的FILE.COPY函数复制函数时,如果文件时打开的,或者在程序中有打开,用该函数时经常出错,想想调用window 的api 函数应该不会出现这种文件,今天就做了一下实验,可以解决该文件
API 有两个复制文件的函数, 第一种(CopyFileEx)可以显示进度条,第二种(CopyFile)没有进度条
//[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
//static extern long CopyFileEx(string lpExistingFileName, string lpNewFileName, long lpProgressRoutine,object lpData,ref long pbCancel, long dwCopyFlags);
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
static extern long CopyFile(string lpExistingFileName, string lpNewFileName, Boolean bFailIfExists);
long lngReturn;
string strFrom = "F://aaa1s1.docx";
string strTo = "F://test//aaa1s1.docx";
long i= 0L;
lngReturn = CopyFile(strFrom, strTo, true);
if (lngReturn == 0)
失败
else
成功
以上是C#代码
VB声明
Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long