C++ CLR 控制台应用程序 [无法获取 Directory::GetCurrentDirectory C3374 的地址]

C++ CLR Console Application [Cant take adress of Directory::GetCurrentDirectory C3374]

我有简单的代码:

msclr::interop::marshal_context ctx;
System::String ^converted = 
    msclr::interop::marshal_as<String^>(Directory::GetCurrentDirectory);
array<String^>^ files = 
    Directory::GetFiles(converted, "*.cpp", System::IO::SearchOption::AllDirectories);

我只是想获取当前目录,然后获取该文件夹和所有子文件夹中的文件。但我收到以下错误:

Error C3374 can't take address of 'System::IO::Directory::GetCurrentDirectory' unless creating delegate instance

我不确定您为什么需要尝试编组这样的方法。尝试直接调用它:

System::String^ converted = Directory::GetCurrentDirectory();
array<String^>^ files = 
    Directory::GetFiles(converted, "*.cpp", System::IO::SearchOption::AllDirectories);
  • Directory::GetCurrentDirectory 已经 returns 一个 System.String^ 所以你不需要编组或转换它。
  • marshal_as 函数用于将托管类型转换为非托管类型,而不是转换值。
  • 您遇到的错误是由于没有调用 Directory::GetCurrentDirectory 和括号,如下所示:Directory::GetCurrentDirectory().