locked
Get FileName RRS feed

  • Question

  • Hey forum,

    I'm using OpenFileDialog to get a .jpg from my users. In a text block beside my "Browse" button, I want the file name (i.e. Pic1.jpg) to be displayed. Using VS 2010, SL4, C#. Code as follows

    private void button1_Click(object sender, System.EventArgs e)
            {
                //Create an instance of the OFD box
                OpenFileDialog dlg = new OpenFileDialog();
    
                //Filter default filetype to be allowed for selection
                dlg.Filter = "Pictures (*.jpg)|*.jpg|All files (*.*)|*.*";
                dlg.FilterIndex = 1;
    
                //Allows multiple files to be selected
                dlg.Multiselect = true;
    
                //Call the ShowDialog method to show the dialog box
                bool? userClickedOK = dlg.ShowDialog();
    
                //Process input if the user clicked OK
                if (userClickedOK == true)
                {
                    //Write file name to textblock
                    StatusText.Text = System.IO.Path.GetFileName(dlg.FileName);
                }
                else
                {
                    StatusText.Text = "No File Selected";
                }
            }


    Issue is:

    //Write file name to textblock
                    StatusText.Text = System.IO.Path.GetFileName(dlg.FileName);

    Any ideas??

    Tuesday, July 12, 2011 5:27 PM

Answers

  • You cannot get the full filename/path from the OpenFileDialog.  The OpenFileDialog does return the file name.

    StatusText.Text = dlg.File.Name;


     

    Tuesday, July 12, 2011 5:36 PM
  • private void Button_Click(object sender, RoutedEventArgs e)
            {
                //Create an instance of the OFD box 
                OpenFileDialog dlg = new OpenFileDialog();
    
                //Filter default filetype to be allowed for selection 
                dlg.Filter = "Pictures (*.jpg)|*.jpg|All files (*.*)|*.*";
                dlg.FilterIndex = 1;
    
                //Allows multiple files to be selected 
                dlg.Multiselect = true;
    
                //Call the ShowDialog method to show the dialog box 
                bool? userClickedOK = dlg.ShowDialog();
    
                //Process input if the user clicked OK 
                if (userClickedOK == true)
                {
                    //Write file name to textblock 
                    StatusText.Text = dlg.File.Name;
                }
                else
                {
                    StatusText.Text = "No File Selected";
                } 
    
            }


    Tuesday, July 12, 2011 6:07 PM

All replies

  • You cannot get the full filename/path from the OpenFileDialog.  The OpenFileDialog does return the file name.

    StatusText.Text = dlg.File.Name;


     

    Tuesday, July 12, 2011 5:36 PM
  • Fury,

    That still gives me an error. But regarding your comment, I don't want path/directory really. I really just want "filename.filetype" I can't get that?

    Tuesday, July 12, 2011 5:53 PM
  • private void Button_Click(object sender, RoutedEventArgs e)
            {
                //Create an instance of the OFD box 
                OpenFileDialog dlg = new OpenFileDialog();
    
                //Filter default filetype to be allowed for selection 
                dlg.Filter = "Pictures (*.jpg)|*.jpg|All files (*.*)|*.*";
                dlg.FilterIndex = 1;
    
                //Allows multiple files to be selected 
                dlg.Multiselect = true;
    
                //Call the ShowDialog method to show the dialog box 
                bool? userClickedOK = dlg.ShowDialog();
    
                //Process input if the user clicked OK 
                if (userClickedOK == true)
                {
                    //Write file name to textblock 
                    StatusText.Text = dlg.File.Name;
                }
                else
                {
                    StatusText.Text = "No File Selected";
                } 
    
            }


    Tuesday, July 12, 2011 6:07 PM
  • As usual, that was right on. I'm sorry I'm retarded and couldn't put the thoughts together to put "File.Name". Thanks for holding my hand! Haha

    Tuesday, July 12, 2011 6:15 PM