Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "jpg files |*.JPG";
dialog.InitialDirectory = "D:\\";
dialog.Title = "Select an Image file";
if (dialog.ShowDialog() == DialogResult.OK)
{
txtLocation.Text = dialog.FileName;
pbxImg.ImageLocation = txtLocation.Text;
txtPath.Text = Environment.CurrentDirectory;
}

pbxImg.Image.Save(Environment.CurrentDirectory+"----.Jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

This is the Code I am Using..

I need to get just the File name from the Openfile dialog. and If I fillup that in the Blanks as a string. I will get the answer.
Posted

Hi,


Refer this link.

here is the sample code for getting selected file from OpenFileDialog,

C#
private void button1_Click(object sender, System.EventArgs e)
{
   if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
   {
      System.IO.StreamReader sr = new 
         System.IO.StreamReader(openFileDialog1.FileName);
      MessageBox.Show(sr.ReadToEnd());
      sr.Close();
   }
}


you can access selected file using openFileDialog.FileName.

hope this is what you want.

thanks
-amit.
 
Share this answer
 
v2
C#
private void button1_Click(object sender, EventArgs e)
{

    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        System.IO.StreamReader sr = new
           System.IO.StreamReader(openFileDialog1.FileName);
        string[] s=(openFileDialog1.FileName.ToString()).Split('\\');
        int count = s.Length;
        MessageBox.Show(s[count-1]);

        sr.Close();
    }

}
 
Share this answer
 
System.IO.Path.GetFileName(Path as String)
 
Share this answer
 
var FD = new System.Windows.Forms.OpenFileDialog();
string fileToOpen = "";
if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileToOpen = FD.FileName;
}

Here fileToOpen contains the filename.
 
Share this answer
 

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