Select file or folder from the same dialog OpenFileDialog in WPF

Mayank
Mayank
Member
194 Points
12 Posts

Hi,

I am using OpenFileDialog to select folder but unable to do so. 

private void btnChangeDestinationPath_Click(object sender, RoutedEventArgs e)
int Uploadedfilecount = 0; 
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect =false;
openFileDialog.Filter ="pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(txtdestinationPath.Text);
if (openFileDialog.ShowDialog() == true)
foreach (string filename in openFileDialog.FileNames)
{
Uploadedfilecount += 1;
txtdestinationPath.Text = System.IO.Path.GetFullPath(filename); 
ConfigurationSettings.AppSettings.Set("MergeDestinationPath", System.IO.Path.GetFullPath(filename));
}
}
}

 

Please help

Views: 14204
Total Answered: 1
Total Marked As Answer: 0
Posted On: 01-Jan-2016 02:19

Share:   fb twitter linkedin
Answers
Brian
Brian
Moderator
2232 Points
14 Posts
         

Hi Mayank,

Use CheckFileExists =false; and set file name to open file dialog as:

private void btnChangeDestinationPath_Click(object sender, RoutedEventArgs e)
int Uploadedfilecount = 0; 
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect =false;
openFileDialog.CheckFileExists =false;
openFileDialog.Filter ="pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(txtdestinationPath.Text);
openFileDialog.FileName = System.IO.Path.GetFileName(txtdestinationPath.Text); 
if (openFileDialog.ShowDialog() == true)
foreach (string filename in openFileDialog.FileNames)
{
Uploadedfilecount += 1;
txtdestinationPath.Text = System.IO.Path.GetFullPath(filename); 
ConfigurationSettings.AppSettings.Set("MergeDestinationPath", System.IO.Path.GetFullPath(filename));
}
}
}

 

Posted On: 01-Jan-2016 04:19
 Log In to Chat