I am developing a simple WPF Image viewer using c#. I am adding a feature so you can right-click on the image to open the file path. I am using Process.Start
to open explorer with the path as the arguments. My app runs fine but won't open explorer.
My c# code is
string selectedFileName;
string directoryPath;
private void MenuItemFromFile_OnClick(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image Files (*.jpg|*.jpg|*.gif|*.ico|*.bmp|*.png|*.wdp| .tiff)|All files (*.*)|*.*";
openFileDialog.FilterIndex = 8;
if (openFileDialog.ShowDialog() == true)
{
selectedFileName = openFileDialog.FileName;
directoryPath = System.IO.Path.GetDirectoryName(openFileDialog.FileName);
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(selectedFileName);
bitmap.EndInit();
FullImage.Source = bitmap;
}
}
private void ContextItemFileLocation_OnClick(object sender, RoutedEventArgs e)
{
Process.Start("C:\Windows\explorer.exe", @directoryPath);
}
My XAML
<Image Grid.Row="2"
Grid.Column="1"
Name = "FullImage"
Stretch="Uniform"
StretchDirection="Both" >
<Image.ContextMenu>
<ContextMenu>
<MenuItem Header="open file location"
Name="ContextItemFileLocation"
Click="ContextItemFileLocation_OnClick"/>
</ContextMenu>
</Image.ContextMenu>
</Image>
question from:https://stackoverflow.com/questions/65661633/process-start-opening-explorer