Process.Start 打开资源管理器

时间:2021-01-11 05:13:19

标签: c# wpf windows rider

我正在使用 c# 开发一个简单的 WPF 图像查看器。我正在添加一个功能,因此您可以右键单击图像以打开文件路径。我正在使用 Process.Start 以路径作为参数打开资源管理器。我的应用运行良好,但无法打开资源管理器。

我的 C# 代码是

        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);
        }

我的 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>

1 个答案:

答案 0 :(得分:0)

我重新启动了计算机,现在它似乎可以正常工作了。还是谢谢你的帮助