Capture file name

Asked

Viewed 4,207 times

1

I have a form that I search the path of a file in .mdb (Access database), however I wanted to know how I can limit and pick up exactly the string of the file between the last bar of the 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 wish it were not necessary to call the last bar of the 5th directory array (because then 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 bar and the .mdb.

2 answers

3

2


You can use the class Fileinfo, in it 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;
  • can turn this Console.WriteLine(fileInfo.Name);in a variable?

  • I will edit the example to use as a variable

Browser other questions tagged

You are not signed in. Login or sign up in order to post.