String 如何解决这个问题;“字符串”;在;对于每一个“;环

String 如何解决这个问题;“字符串”;在;对于每一个“;环,string,c++-cli,managed-c++,String,C++ Cli,Managed C++,我的任务是在单击按钮后打开多个文件,并读取所有选定的文件 我在C语言中找到了一个FuleAch函数的例子,但是我想用C++编写。我应该如何转换它呢 它显示了一个错误,System::String,如果没有顶级“^”,则无法在此处使用此类型 我还是个新手。有人能提出建议吗 多谢各位 下面是我写的代码 Stream^ myStream; OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog; openFileDialog1->Initi

我的任务是在单击按钮后打开多个文件,并读取所有选定的文件

<>我在C语言中找到了一个FuleAch函数的例子,但是我想用C++编写。我应该如何转换它呢

它显示了一个错误,
System::String
,如果没有顶级“^”,则无法在此处使用此类型

我还是个新手。有人能提出建议吗

多谢各位

下面是我写的代码

Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Title = "open captured file";
openFileDialog1->Filter = "CP files (*.cp)|*.cp|All files (*.*)|*.*|txt files (*.txt)|*.txt";
openFileDialog1->FilterIndex = 2;
//openFileDialog1->RestoreDirectory = true;
openFileDialog1->Multiselect = true;

if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
    **for each (String line in openFileDialog1->FileName)** 
         System::Diagnostics::Debug::WriteLine("{0}",line);
    myStream->Close();
}

根据返回的容器类型,在
System::Array::ForEach
OpenFileDialog::SafeFileNames

中定义了
std::for_each
,这属于
C++-STL
,因此是可移植的。你可以在那里使用,不用担心。这是唯一的办法吗?在我浏览了你的链接之后,我真的不明白这一点。抱歉,如果这个问题有点愚蠢,因为我对它还很陌生。无论如何,非常感谢您抽出时间。嗯,
std::foreach
最接近您想要的。这样看:在
C#
中作为
std::foreach
的第三个参数提供的
foreach(){}
中提供的内容。当然,会有使用迭代器的正常方法。
#include <algorithm>

std::for_each(InputIterator first, InputIterator last, Function fn);
System::Action<T> myAction; // You will Need to provide a function here to be used on every filename.
System::Array::ForEach(openFileDialog1->SafeFileNames, myAction);