public Form1()
{
InitializeComponent();
if (textBoxRadarPath.Text != "")
{
if (!Directory.Exists(textBoxRadarPath.Text))
{
Directory.CreateDirectory(textBoxRadarPath.Text);
btnStart.Enabled = true;
}
}
if (textBoxSatellitePath.Text != "")
{
if (!Directory.Exists(textBoxSatellitePath.Text))
{
Directory.CreateDirectory(textBoxSatellitePath.Text);
btnStart.Enabled = true;
}
}
CheckIfImagesExist();
}
I'm checking if the folders in textBoxRadarPath.Text and textBoxSatellitePath.Text exist and create them if not. Then I call the method CheckIfImagesExist();
The problem is in the folder CheckIfImagesExist(); I'm also trying to get files : There is more code in the method but the important is the GetImagesFiles method
private void CheckIfImagesExist()
{
GetImagesFiles();
}
And in GetImagesFiles
private void GetImagesFiles()
{
if (textBoxRadarPath.Text != "" || textBoxSatellitePath.Text != "")
{
if (Directory.Exists(textBoxRadarPath.Text))
{
if (checkBoxGetImages)
{
filesRadar = System.IO.Directory.GetFiles(textBoxRadarPath.Text,
"*.gif", SearchOption.AllDirectories).OrderBy(x => x).ToArray();
}
else
{
var t = new DirectoryInfo(textBoxRadarPath.Text).GetDirectories()
.OrderByDescending(d => d.LastWriteTimeUtc).First();
filesRadar = System.IO.Directory.GetFiles(t.FullName,
"*.gif", SearchOption.AllDirectories).OrderBy(x => x).ToArray();
}
Array.Sort(filesRadar, new MyComparer(true));
}
if (Directory.Exists(textBoxSatellitePath.Text))
{
if (checkBoxGetImages)
{
filesSatellite = System.IO.Directory.GetFiles(textBoxSatellitePath.Text,
"*.gif", SearchOption.AllDirectories).OrderBy(x => x).ToArray();
}
else
{
var t = new DirectoryInfo(textBoxSatellitePath.Text).GetDirectories()
.OrderByDescending(d => d.LastWriteTimeUtc).First();
filesSatellite = System.IO.Directory.GetFiles(t.FullName,
"*.gif", SearchOption.AllDirectories).OrderBy(x => x).ToArray();
}
Array.Sort(filesSatellite, new MyComparer(false));
}
}
}
Here I'm trying to get images from the first child folder under textBoxRadarPath.Text and textBoxSatellitePath.Text
The problem is if the directories textBoxRadarPath.Text and textBoxSatellitePath.Text not exist so they have created but they are empty yet so I'm getting exception on the line
var t = new DirectoryInfo(textBoxRadarPath.Text).GetDirectories()
.OrderByDescending(d => d.LastWriteTimeUtc).First();
Because the two directories just created or they are empty.
How can I check in the constructor if the directories are empty don't call the method CheckIfImagesExist() ?
Only if there are child folders inside then call the method CheckIfImagesExist()
The exception message :
System.InvalidOperationException: 'Sequence contains no elements'
On the line :
var t = new DirectoryInfo(textBoxRadarPath.Text).GetDirectories()
.OrderByDescending(d => d.LastWriteTimeUtc).First();
This screenshot show example of the folders structure on the hard disk.
CodePudding user response:
Use FirstOrDefault when you are not sure there will be a resulting item
var t = new DirectoryInfo(textBoxRadarPath.Text)
.GetDirectories()
.OrderByDescending(d => d.LastWriteTimeUtc)
.FirstOrDefault(); // <- this will return null if there are no entries
then before you start checking the files, make sure you found a directory.
if (t != null)
{
filesSatellite = System.IO.Directory
.GetFiles(t.FullName, "*.gif", SearchOption.AllDirectories)
.OrderBy(x => x)
.ToArray();
}