I have a OpenDialog
in my wpf application where the user can choose file and save to folder. I want to Save the image to specific folder and set the filename & extenstion on button click in wpf.
Folder structure:
-MyAppDirectory
--ContactImages
-1.jpg
When i execute following code it create the "ContactImages
" direcotry in Bin folder and not in Application main direcotry. Any idea? & how to get file extension of uploaded file in wpf & set file name?
in xaml.cs file:
private void imgContactImage_MouseDown(object sender, MouseButtonEventArgs e)
{
string folderpath = Environment.CurrentDirectory + "\\ContactImages\\";
op.Title = "Select a picture";
op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
bool? myResult;
myResult = op.ShowDialog();
if (myResult != null && myResult == true)
{
imgContactImage.Source = new BitmapImage(new Uri(op.FileName));
if (!Directory.Exists(folderpath))
{
Directory.CreateDirectory(folderpath);
}
//System.IO.File.Copy(op.FileName,filename);
}
}
You can rewrite the snippet provided as
OpenFileDialog op = new OpenFileDialog();
string folderpath = System.IO.Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + "\\ContactImages\\";
op.Title = "Select a picture";
op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
bool? myResult;
myResult = op.ShowDialog();
if (myResult != null && myResult == true)
{
imgContactImage.Source = new BitmapImage(new Uri(op.FileName));
if (!Directory.Exists(folderpath))
{
Directory.CreateDirectory(folderpath);
}
string filePath = folderpath + System.IO.Path.GetFileName(op.FileName);
System.IO.File.Copy(op.FileName, filePath, true);
}