我在代码中设置了一个多选功能,允许我打开多个“.txt”形式的文件。这就是问题,在通过OpenFileDialog打开后,我将如何读取所有这些选定的文件?以下代码和“for each”行,当我使用System :: Diagnostics :: Debug时,它只显示文件中的数据,而其他文件的数据则丢失。我应该如何在“for each”之后修改代码?有人可以提供一些建议或建议吗?选择的文件为1_1.txt,2_1.txt,3_1.txt。感谢您的回复,并提前致谢。
这是我的书面代码,
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->Multiselect = true;
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
array<String^>^ lines = System::IO::File::ReadAllLines(openFileDialog1->FileName);
for each (String^ line in lines) {
//?????
System::Diagnostics::Debug::WriteLine("",line);
}
}
答案 0 :(得分:1)
如果允许选择多个文件,则需要查看OpenFileDialog.FileNames
属性:
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
for each (String^ file in openFileDialog1->FileNames)
{
array<String^>^ lines = System::IO::File::ReadAllLines(file);
for each (String^ line in lines)
{
System::Diagnostics::Debug::WriteLine("",line);
}
}
}
答案 1 :(得分:0)
使用FileNames
属性。
C#版本(应该很容易适应C ++):
foreach (var file in openFileDialog1.FileNames)
{
foreach (var line in File.ReadAllLines(file)
{
...
}
}
答案 2 :(得分:0)
使用openFileDialog1->FileNames
。它返回您选择的多个文件名
请阅读此处 http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.multiselect.aspx 它在C#中,但很容易推断为C ++。