Anna
Anna

Reputation: 69

C# How to display only file name in dataGridView without full path in Winforms

I want to display only filename with extenstion .pdf, but this code shows me fullpath plus filename.pdf , but I want to display only filename.pdf

Hers is my code and thank you in advance.

string installedPath = Application.StartupPath + "pdfFiles\\" + PatId.ToString() + "\\" + Regnr; 
            String[] files = Directory.GetFiles(installedPath);
            DataTable table = new DataTable();
            table.Columns.Add("File name");

            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = new FileInfo(files[i]);
                table.Rows.Add(file);
            }
            dgvFiles.DataSource = table;

Upvotes: 0

Views: 134

Answers (1)

szybki
szybki

Reputation: 114

Maybe not the most professional solution, but a string.Split should do the work

for (int i = 0; i < files.Length; i++)
        {
            string[] temp = files[i].Split('\\');
            string fileName = temp.Last();
            FileInfo file = new FileInfo(fileName);
            table.Rows.Add(file);
        }

Upvotes: 1

Related Questions