简体   繁体   中英

Process.Start opening explorer

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM