Manjunath ST Manjunath ST - 2 months ago 7
C# Question

How to show message if folder contains different extensions along with my .Dat file?

I have to show a message, if my folder contains .Dat files and other
extension files. how to do this . Need help appreciated

private void btnChooseFile_Click(object sender, EventArgs e)
{
FileInfo[] files;
FolderBrowserDialog fbd = new FolderBrowserDialog();
//fbd.RootFolder = Environment.SpecialFolder.MyComputer;
fbd.SelectedPath = @txtFilepath.Text.Trim();
if (fbd.ShowDialog() == DialogResult.OK)
{
txtFilepath.Text = fbd.SelectedPath;
if (rbtnOffline.Checked)
{
DefaultManager.OfflineFilePath = @txtFilepath.Text.ToString().Trim();
DirectoryInfo info = new DirectoryInfo(DefaultManager.OfflineFilePath);
files = info.GetFiles("*.dat").OrderBy(p => p.LastWriteTime).ToArray();

if (files.Count() > 0)
{
// MessageBox.Show("no error");
}
else
{
MessageBox.Show("Error, Incorrect capture folder selected", "PGY-SSM", MessageBoxButtons.OK, MessageBoxIcon.Error);
//txtFilepath.Clear();
}
}
else
{
DefaultManager.DumpFilePath = @txtFilepath.Text.ToString().Trim();
}
}}

Answer

To determine if there are any other files in the folder, compare the total number of files in the folder with the number of dat files:

int fileCount = info.GetFiles().Length;
int datFileCount = info.GetFiles("*.dat").Length;

if (fileCount != datFileCount)
{
      MessageBox.Show("Error, other files found …");
}