-1

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>
Lanzelot
  • 9
  • 2
  • 2
    Can you describe what exactly happens when you click the `ContextItem`? It's wired because I've tested the code and it's working fine for me. Have you tried debugging to check the `directoryPath` value before you run `Process.Start()`? – Ahmed Zaki Jan 11 '21 at 05:37
  • The directory path is what it is supposed to be when debugging, when I click on the context menu, it closes and nothing happens – Lanzelot Jan 11 '21 at 17:17
  • Are you sure you've selected the image first before you try to open the image location? Please put a BreakPoint right before `Process.Start()` and debug to check the value of `directoryPath` and even whether or not the event handler `ContextItemFileLocation_OnClick` is called. – Ahmed Zaki Jan 11 '21 at 17:56

1 Answers1

0

I restarted my computer and it seems to be working now. Thanks for the help anyway

Lanzelot
  • 9
  • 2