c# - Display File name in Label Text -


i'm using winforms. in form have picturebox. on form load application opens png picture specific folder inside computer. want able display file name in label.

for example location is: c:\image\

the label should say:

c:\image\mypicture.png

    private void form1_load(object sender, eventargs e)     {          try // tif file c:\image\ folder         {             string path = @"c:\image\";             string[] filename = directory.getfiles(path, "*.png");              picturebox1.load(filename[0]);               lblfile.text = path; //i've tried this... not give file name          }         catch(exception ex)         {             messagebox.show("no files or " + ex.message);         }     } 

you have no need files (directory.getfiles), 1st one, let's get rid of array , simplify code:

private void form1_load(object sender, eventargs e) {      try // tif file c:\image\ folder     {         string path = @"c:\image\";         string filename = directory.enumeratefiles(path, "*.png").firstordefault();          if (null != filename) {           // load picture            picturebox1.load(filename);           // show file name           lblfile.text = filename;         }         else {           //todo: no *.png files found         }      }     catch(ioexception ex)     {         messagebox.show("no files or " + ex.message);     } } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -