Click here to Skip to main content
15,920,468 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am currently doing one program which displays in datagrid name of the file you choose but I used this way:

dataGridView1.Rows.Insert(0, "Screenshoot 1", "Uploading", Convert.ToString(imageFileDialog.FileName));

And this gives complete path example: C:\User\Imagename.png

How to make to it will only give Imagename.png nothing else?

Thanks for your answers!
Posted
Updated 30-Apr-11 8:16am
v3

Hi, try this:

Convert.ToString(imageFileDialog.SafeFileName);
 
Share this answer
 
Comments
RaviRanjanKr 30-Apr-11 14:39pm    
Nice Answer! My 5 :)
Tarun.K.S 1-May-11 9:09am    
Thanks Ravi! :)
Sergey Alexandrovich Kryukov 30-Apr-11 17:26pm    
Correct, a 5.
--SA
Tarun.K.S 1-May-11 9:09am    
Thanks SA! :)
Use this code

C#
System.IO.Path.GetFileName(imageFileDialog.FileName);
 
Share this answer
 
Comments
RaviRanjanKr 30-Apr-11 14:39pm    
Good Answer, My 5 :)
Wonde Tadesse 6-Dec-11 10:29am    
Thanks
Sergey Alexandrovich Kryukov 30-Apr-11 17:26pm    
Correct, a 5.
--SA
Wonde Tadesse 6-Dec-11 10:29am    
Thanks
you already got some expert advice but you can also use RegularExpressions (using Regex) for that
string filename = Regex.Match(imageFileDialog.FileName, @"[^\\]*$").Value;
Convert.ToString(filename);

when you implement it on your code
C:\User\Imagename.png l will be Imagename.png
 
Share this answer
 
Comments
Wonde Tadesse 30-Apr-11 14:55pm    
Thumbs up. You've got 5 of mine. This also another option.
Tarun.K.S 30-Apr-11 14:55pm    
Good use of Regex. 5+ :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900