This is a user control i wrote to move open file dialogue out of my code.
Set properties to access the Filename, dirname, full path and various other bits of file info that may be useful.
public partial class FileUploads : UserControl
{
public string FileName { get; set; }
public string DirectoryName { get; set; }
public string FullPath { get; set; }
public string Extension { get; set; }
public string FileNameNoExt { get; set; }
public FileUploads()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "(*.GIF;*.JPG;*.JPEG;*.PDF;*.PNG;*.MP3;*.TIF;*.WAV;*.WMA)|*.GIF;*.JPG;*.JPEG;*.PDF;*.PNG;*.MP3;*.TIF;*.WAV;*.WMA";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDialog1.FileName;
FileName = Path.GetFileName(openFileDialog1.FileName);
FileNameNoExt = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
DirectoryName = Path.GetDirectoryName(openFileDialog1.FileName);
FullPath = Path.GetFullPath(openFileDialog1.FileName);
Extension = Path.GetExtension(openFileDialog1.FileName);
}
}
}