我有以下函数,它有望告诉我文件夹是否存在,但是当我调用它时,我得到了这个错误-
cannot convert parameter 1 from 'System::String ^' to 'std::string'
函数-
#include <sys/stat.h>
#include <string>
bool directory_exists(std::string path){
struct stat fileinfo;
return !stat(path.c_str(), &fileinfo);
}
调用(来自保存用户选择文件夹的表单的 form.h 文件)-
private:
System::Void radioListFiles_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
if(directory_exists(txtActionFolder->Text)){
this->btnFinish->Enabled = true;
}
}
谁能告诉我如何解决这个问题?谢谢。
最佳答案
您正在尝试将托管的 C++/CLI 字符串 (System::String^
) 转换为 std::string
。没有为此提供隐式转换。
为了让它工作,你必须处理 string conversion yourself .
这可能看起来像:
std::string path = context->marshal_as<std::string>(txtActionFolder->Text));
if(directory_exists(path)) {
this->btnFinish->Enabled = true;
}
也就是说,在这种情况下,完全坚持使用托管 API 可能更容易:
if(System::IO::Directory::Exists(txtActionFolder->Text)) {
this->btnFinish->Enabled = true;
}
关于c++ - 将 'system::string' 传递给函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15168276/