Checking file type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I 'm using drag-n-drop and if the user drops a file, I want to make sure
that it is either an Excel file or a plain text file.

Is the right way to do this to check that the last three chars of the file
name are either xls or txt, or is there another way?

Thanks,
-Flack
 
Flack said:
Hello,

I 'm using drag-n-drop and if the user drops a file, I want to make sure
that it is either an Excel file or a plain text file.

Is the right way to do this to check that the last three chars of the file
name are either xls or txt, or is there another way?

Thanks,
-Flack

I would check the extension, rather than the last three chars of the
filename:

IE:

using System.IO;

namespace MyNamespace
{
public class Class1
{
public bool IsTextFile(string FileName)
{
string ext = Path.GetExtension(FileName).ToLower();
return (ext == ".txt" || ext == ".csv");
}

public bool IsExcelFile(string FIleName)
{
string ext = Path.GetExtension(FileName).ToLower();
// Add the rest of the excel file extensions you want to the
list below.
return (ext == ".xls" || ".xlt" || ".xla" || ".xlm");
}
}
}

HTH,
Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top