c++ - 将 'system::string' 传递给函数时出错

标签 c++ string c++-cli

我有以下函数,它有望告诉我文件夹是否存在,但是当我调用它时,我得到了这个错误-

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/

相关文章:

thread-safety - C++/CLI 中的 CRITICAL_SECTION

c++ - 有没有办法跳到代码的特定部分进行调试?

c# - 在 C# 中使用 pragma intrinsic(sqrt, pow)?

c++ - 复制构造函数奇怪的编译错误

c# - String.Equals GID 返回 false?

python - 使用python的re模块分割字符串

generics - C++/CLI 泛型,在 array<> 和其他集合中使用 T

c++ - 删除类列表的静态成员的元素

c++ - 如何在C++中比较两个文本文件

c# - C++/CLI 中 C# Array[] 的语法