If your current code is failing, then knowing what exceptions are getting thrown is essential. Before you do anything else, put a Debug::WriteLine in your catch block and see what exceptions you're getting!
However, the method you have listed is near-guaranteed to give you problems on large files. You're reading the entire file contents into memory, then converting the byte array into a Unicode string, resulting in a 6x expansion, then removing dashes from the string, resulting in total memory usage of 10x the original file size.
Instead, read the bytes in and check them as they're read. Perhaps something like this:
array<Byte>^ target = { 0x55, 0x50, 0x58, 0x30 };
try
{
int matched = 0;
FileStream^ fs2 = gcnew FileStream(line, FileMode::Open, FileAccess::Read, FileShare::ReadWrite)
int value;
do
{
value = fs2->ReadByte();
if(value == target[matched])
matched++;
else
matched = 0;
if(matched == target->Length)
{
listBox1->Items->Add(line);
break;
}
} while (value != -1);
fs2->Close();
}
catch(Exception^ e)
{
Debug::WriteLine("Skipping file: " + line + " due to " + e->Message);
}
You'll still get exceptions trying to open operating system files, but at least you'll have some idea what the exceptions are, and you'll be able to handle large files without problems.