If you selected only one file, you dont need a foreach loop. You use it when you have multiple files.
So what you can do is this:
public Form1()
{
InitializeComponent();
label1.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.png; *.jpg; *.bmp)|*.png; *.jpg; *.bmp";
open.InitialDirectory = @"C:\";
if (open.ShowDialog() == DialogResult.OK)
{
System.IO.FileInfo fInfo = new System.IO.FileInfo(open.FileName);
StringBuilder sb = new StringBuilder();
sb.AppendLine("File`s specifications are:\n");
sb.AppendLine("Full file path: " + fInfo.FullName);
sb.AppendLine("Full file name: " + fInfo.Name);
sb.AppendLine("File name only: " + System.IO.Path.GetFileNameWithoutExtension(fInfo.FullName));
sb.AppendLine("File extenstion: " + fInfo.Extension.ToString().Substring(1, fInfo.Extension.Length - 1)); //removing dot infront of extension!
sb.AppendLine("File lenght: " + (String.Format("{0:0.00} MB", (((decimal)fInfo.Length / 1024) / 1024))));
//you can add some more file`s properties if you want simply...
label1.Text = sb.ToString();
}
} Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474