java copyfile失败_如何修复CopyFile()错误5 - 访问被拒绝错误

我正在尝试编写可在Linux和Windows上使用的复制文件功能 . 它适用于Linux,但在Windows上,我在尝试使用WinApi函数CopyFile()时收到错误代码5 .

In header File.h

这是我应该能够在Linux和Windows上使用的File命名空间中的自定义函数 .

class File

{

public:

static bool copyFile(std::string source, std::string destination);

private:

}

In File.cpp

对于Linux来说很简单:

#ifdef __unix__

#include "File.h"

bool File::copyFile(std::string source, std::string destination)

{

std::string arg = source + " " + destination;

return launchProcess("cp", arg);

}

#endif

在Windows特定的代码块中,我使用WinAPI(#include )函数CopyFile() . 这接受LPCWSTR数据类型而不是字符串 . 为了解决这个问题,我创建了一个将字符串转换为LPCWSTR类型的函数 .

#ifdef _WIN32

#include "File.h"

#include

std::wstring strtowstr(const std::string &str)

{

// Convert an ASCII string to a Unicode String

std::wstring wstrTo;

wchar_t *wszTo = new wchar_t[str.length() + 1];

wszTo[str.size()] = L'\0';

MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wszTo,(int)str.length());

wstrTo = wszTo;

delete[] wszTo;

return wstrTo;

}

bool File::copyFile(std::string source, std::string destination)

{

std::wstring wsource = strtowstr(source);

std::wstring wdestination = strtowstr(destination);

int result = CopyFileW(wsource.c_str(), wdestination.c_str(), TRUE);

//for debugging...

std::wcout << "The error is " << GetLastError() <<:endl>

std::wcout << wsource.c_str() << std::endl;

std::wcout << wdestination.c_str() << std::endl;

if (result == 0)

{

return false;

}

return true;

}

#endif

In my Test Programme

TEST(all,main_copy_file)

{

std::cout << "Testing copyFile() function..." << std::endl;

std::string srcDir = File::currentWorkingDirectory() + "srcDir";

File::makeDirectory(srcDir);

std::string destDir = File::currentWorkingDirectory() + "destDir/";

File::makeDirectory(destDir);

File::makeFile(srcDir, "testFile", ".txt");

ASSERT_TRUE(File::fileExists(srcDir + "/testFile.txt")) << "Error: Test file has not been generated" << std::endl;

ASSERT_TRUE(File::directoryExists(destDir)) << "Error: Destination directory does not exist" <<:endl>

ASSERT_TRUE(File::copyFile(srcDir + "/testFile.txt", destDir)) << "Error: Coppy unsucsessfull" << std::endl;

ASSERT_TRUE(File::fileExists(destDir + "/testFile.txt")) << "Error: CoppyFile() flagged as sucsessfull but file does not exist" << std::endl;

}

In the application Output (on Windows)

/*

Testing copyFile() function...

The error is 5

C:\GIT\CorteX\Externals\OSAL\build\Debug/srcDir/testFile.txt

C:\GIT\CorteX\Externals\OSAL\build\Debug/destDir/

error: Value of: File::copyFile(srcDir + "/testFile.txt", destDir)

Actual: false

Expected: true

Error: Coppy unsucsessfull

*/

错误代码5是访问被拒绝错误 . 我认为当目录不存在,目录在其他地方打开,或者我没有权限时,它会出现此错误 .

由于我已经测试过该目录确实存在,我认为它必须是后两者之一 . 我可能只限制了管理员权限(我不知道),但我可以在没有管理员权限的情况下粘贴到“destDir” . 那么也许它认为该目录是开放的?是否存在确保目录已关闭的命令?

在Linux上运行时,测试成功 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值