-2

I have the following method in windows form

private void btnSelectNuevo_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string fichero = openFileDialog2.FileName.ToString();

                    txtbox_sel_fich.Text = fichero;
                    if (string.IsNullOrEmpty(txtbox_sel_fich.Text) == true)
                    {
                        MessageBox.Show("file not selected", "return file selection", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (SecurityException ex)
                {
                    MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" +
                    $"Details:\n\n{ex.StackTrace}");
                }
            }
        }

and another method:

private void btnsubir_Click(object sender, EventArgs e)
        {

           string reply = "";
           string fichero = "";
           fichero = textbox_select.Text.ToString();
           // string ruta = textbox_select.Text.ToString();
           // FileInfo fich = new FileInfo(ruta);
           // fichero = fich.Name;


                if (fichero == readLastFile(fichero))
                {
                    createLog(fichero + ":this file" + fichero + " You have already been imported previously. No action is realized\n");
                     MessageBox.Show("This file" + "'" + fichero + "'" + " You have already been imported");
                }
                else
                {..

how can you see I have a method called "readLastFile (fichero)"; what it does is check in MySql in the "File" table if there is a file already imported with that name that I pass as a parameter, if it exists it does not import it to the DDBB and the program ends but if it does not exist it does the import. The problem I have is that the file variable stores the entire path of an excel file (c: \ ... \ helloWord.xlsx) when what I want is that it only stores (helloWord.xlsx) since when I am going to do the check tells me it doesn't exist and starts loading when it shouldn't.

Some help? I don't know if I am saving it correctly. THANKS FOR YOUR SUPPORT

Fyah10
  • 49
  • 6
  • Please review this SO question https://stackoverflow.com/questions/6921105/given-a-filesystem-path-is-there-a-shorter-way-to-extract-the-filename-without – vscoder Jun 30 '21 at 22:39

1 Answers1

0

I have is that the file variable stores the entire path of an excel file (c: \ ... \ helloWord.xlsx) when what I want is that it only stores (helloWord.xlsx)

Use Path.GetFileName(...)

Path is part of the System.IO namespace

Caius Jard
  • 72,509
  • 5
  • 49
  • 80