Get a file name and display on a label

Asked

Viewed 77 times

0

I am doing a project and need that when opening an (image) it will take the name of this image, save in a variable and show in one of the excel line.

Note: This will be repeated, there will be several files that I will open..

So I know I need to create a foreach to get a new line inserted for every new image I open. Another way is not to save to an array and any image that opens to clear the variable.

Button that opens the image:

private void openFileDialog_FileOk(object sender, CancelEventArgs e)
{
try
{
string[] caminhoImagens = Directory.GetFiles(@"C:\Users\pedroduca\Pictures", "*.jpg");
Path.GetFileNameWithoutExtension(caminhoImagens[0]);
string nomeArquivo = caminhoImagens[0];
NomeImagem.Text = nomeArquivo;

Bitmap image = new Bitmap(openFileDialog1.FileName);
RFiltro1.Text = empty;
RFiltro2.Text = empty;
RFiltro3.Text = empty;

pictureBox1.Image = image;
}
catch (Exception ex)
{
MessageBox.Show($"Erro: {ex.Message}");
}
} 

Here is the button to generate excel:

private void button20_Click(object sender, EventArgs e)
 {
Excel.Application AbreExcel = new Microsoft.Office.Interop.Excel.Application();

if (AbreExcel == null)
{
MessageBox.Show("O Excel não está instalado corretamente!");
return;
}
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

xlWorkBook = AbreExcel.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

xlWorkSheet.Cells[1, 1] = "Nome do arquivo";
xlWorkSheet.Cells[1, 2] = "Teste 1";
xlWorkSheet.Cells[1, 3] = "Teste 2";
xlWorkSheet.Cells[1, 4] = "Teste 3";
xlWorkSheet.Cells[2, 2] = Lstr_RFiltro1;
xlWorkSheet.Cells[2, 3] = Lstr_RFiltro2;
xlWorkSheet.Cells[2, 4] = Lstr_RFiltro3;
xlWorkSheet.Cells[2, 1] = "**AQUI VAI O NOME DO ARQUIVO NA ARRAY**";

xlWorkBook.SaveAs("C:\\Users\\pedroduca\\Pictures\\TesteCaptcha.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
AbreExcel.Quit();

Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(AbreExcel);

MessageBox.Show("Excel criado com sucesso! ");
}

Sorry for the misshaping, I’m new to stackoverflow.

  • But after all what do you want to do? What’s going on? What’s the problem?

  • I wanted to save the name of the files in an array type and that was displayed in a Label. After all I ended up saving the name of the files, I removed the Label and saved in Excel.

1 answer

0


You must pass the file name in order to manipulate it.

In your case what you can do is get the files from the directory, and from that filter and get the file you want.

Add this namespace:

using System.IO;

And with the following code, get the images in the directory you specify:

string[] caminhoImagens = Directory.GetFiles(@"c:\Imagens\", "*.png",
                                     SearchOption.TopDirectoryOnly);

With this it will get a list with the complete path of all images from that directory, for example: c: Images imagem01.png - c: Images imagemPequena03.png

I hope I’ve helped.

  • I edited the code, I even used the code. But he took the file directory. In my Label for example it was: C: Users pedroduca Pictures imagem1.png And I needed it to be only imagem1. I got it with Path.Getfilenamewithoutextension(wayImagens[0]);

Browser other questions tagged

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