المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Filename check with extention



C# Programming
05-05-2009, 02:29 PM
Hi All,
I have browse button to select a .hex file, this file name should always have 3 underscores and .hex as extention (for eg. TestHex_1_1_1.hex). I had written a code by using split & it works. But if foldername contains any underscore then it will not work.
This is my code.
private void btnOpenFolder1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
openFileDialog1.ValidateNames = true;
textBox1.Text = openFileDialog1.FileName;

char[] cr ={ '_' };
string[] str = textBox1.Text.Split(cr);

if (str.Length != 4 || textBox1.Text.Substring(textBox1.Text.LastIndexOf("."), 4).ToLower() != ".hex")
{
textBox1.Text = "";
MessageBox.Show("File name or file extention is not valid.");
}
hexPath = textBox1.Text;

}
Please give me any suggestion.