Capture file name

1

I have a form that I look for the path of a file in .mdb (Access database), however I wanted to know how I can limit and exactly get the string of the file between the last bar of OpenFileDialog and the point of .mdb .

So far I've made this format:

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "Database Files|*.mdb";

    if (dlg.ShowDialog() == DialogResult.OK)
    {
         string dbfile = dlg.FileName;
         label1.Text = dbfile;

         string file_name = dbfile.Split('\')[5];

         string first = file_name.Split('.').FirstOrDefault();
    }
}

I would like it not necessary to call the last directory bar of the 5th array (because the item could be in any folder, not necessarily in the fourth folder of the computer), but a dynamic data that could read the last one bar and .mdb .

    
asked by anonymous 01.08.2016 / 15:26

2 answers

2

You can use the FileInfo class , you will find several methods to work with files

FileInfo fileInfo = new FileInfo(dbfile);
//Mostra o nome do arquivo
string fileName = fileInfo.Name;
//Mostra a extensão do arquivo
string fileExtension = fileInfo.Extension;
//Mostra o caminho completo do arquivo junto com o nome
string fileFullName = fileInfo.FullName;
    
01.08.2016 / 15:38
2

The best way is to use a ready-made method like GetFileNameWithoutExtension() .

    
01.08.2016 / 15:44