Hi All!
I have a problem with copying a local file to a network share using the CopyFile function (in an MFC application).
{ const DWORD maxPathLength = 256; TCHAR tempPath[maxPathLength]; if (!GetTempPath(maxPathLength, tempPath)) return; CString tempFolder = tempPath; CString targetFolder = _T("\\\\ComputerName\\Upload\\"); CString source = _T("E:\\Document.txt"); CString tmpFilePath = tempFolder +_T("TestFileCopy.txt"); // Test to show that before the copy to temp folder it works fine if (!CopyFile(source, targetFolder + _T("TestFileCopy_BEFORE.txt"), TRUE)) return; // After this copy operation the copy to the target folder wont work if (!CopyFile(source, tmpFilePath, FALSE)) return; // For the first time the copy will create only a 0 KB file if (!CopyFile(source, targetFolder + _T("TestFileCopy_FIRST.txt"), TRUE)) { DWORD err = GetLastError(); TRACE(_T("Failed to copy. Error code: %d"), err); // For the second time the copy will work fine if (!CopyFile(source, targetFolder + _T("TestFileCopy_SECOND.txt"), TRUE)) return; } DeleteFile(tmpFilePath); }
The target folder \\ComputerName\Upload has special permissions.
- "This Folder Only" permission: Create files
- "File Only" permissions: write data, append data, Write attributes, Write extended attributes
The problem is that before copying the file to the network share I have to make a copy of it to the local temp folder which makes the copy to the network share to fail.
The first CopyFile which copies the file to the network share is always successful. But the thirdCopyFile function (which should create the TestFileCopy_FIRST.txt file) always fails (it creates a 0 KB file in the target folder). The fourth function call works fine again.
If I comment out the second CopyFile function call which is needed because of the copy to the temp folder, than the next function call will successfully copy the source file to the TestFileCopy_FIRST.txtfile (with valid size/data).
I tried to figure out what is happening and used the ProcessMonitor application to see a bit more. The only thing what I could see is that during the thirdCopyFile call the TestFileCopy_FIRST.txt CreateFile call contained an extra option (compared to the other calls) named:Synchronous IO Non-Alert. And after a QueryDeviceInformationVolume operation theCloseFile has been called. After this the file can not be opened again because the user has only write permissions =>ACCESS DENIED returned.
Does anyone have a solution for this issue?
Thank you in advance.
Laszlo