I want to declare a string array, I was using this way of doing it:
string[] matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
which worked perfectly.
Now I want to enclose the Directory.GetFiles call in a try/catch block, but I can't also have the declaration of the string array in there because then it won't be in the right scope to use it outside of the try block. But if I try this:
string[] matchingActiveLogFiles;
try
{
matchingFiles = Directory.GetFiles(FilePath, FileNamePattern);
}
catch (Exception ex)
{
//log error
}
I have not initialized the string array so I have an error. So I am wondering what is best practise in this situation, should I declare the string array outside the try block? And if so how?