c++ returning filename of first file in directory

i need to return the filename of the first file in a directory

1
2
3
String ^location="";    //root directory
array<String^> ^files = Directory::GetFiles(location);
label4->Text=files[0]


I keep getting unhandled exception. The issue is with label4->Text=files[0] since when I remove it there is no error.

Note: the directory has some files in it so it is not empty. Also: What will files[0] be if the directory is empty?
This looks like some kind of C++ variant; possibly managed C++.

What is label4? What does Directory::GetFiles do? I think you might be better off on a Managed C++ forum.

Are you allowed to have an empty string for the location??

Try putting in an actual path like:
String ^location="C:\\"; //root directory
actually it looks a little like objective-c

i think.
Last edited on
Nah, It's just Microsoft C++/CLI
Problem was solved, i used a file named ID and it there was the name of first file.
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using namespace System;
using namespace System::IO;
using namespace System::Collections;


int main(array<System::String ^> ^args)
{
    String^ path = gcnew String("C:\\");

    array<String^>^fileEntries = Directory::GetFiles( path );

    IEnumerator^ files = fileEntries->GetEnumerator();
    if(files->MoveNext() )
    {
        String^ fileName = safe_cast<String^>(files->Current);
        Console::WriteLine( "'{0}'.", fileName );
    }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.