简体   繁体   English

Process.Start("IExplore.exe"); --> System.IO.FileNotFoundException

[英]Process.Start("IExplore.exe"); --> System.IO.FileNotFoundException

Been trying to run this simple code is it's driving me nuts as it doesn't work.一直在尝试运行这个简单的代码,它让我发疯,因为它不起作用。

I was also expecting Visual Studio to break the program in the line where I get the error but it breaks on the function call and not inside of the function as you can see in the picture bellow.我还期望 Visual Studio 在出现错误的行中中断程序,但它在 function 调用中中断,而不是在 function 内部中断,如下图所示。

        OpenFolder();

        private void OpenFolder()
        {
            Process.Start("explorer.exe", @"c:\temp");

        }

And I get:我得到:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Diagnostics.Process, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. System.IO.FileNotFoundException:'无法加载文件或程序集'System.Diagnostics.Process,版本 = 4.1.0.0,文化 = 中性,PublicKeyToken = b03f5f7f11d50a3a'。 The system cannot find the file specified.'该系统找不到指定的文件。'

在此处输入图像描述

Cause of the problem: Because UseShellExecute defaults to false on .NET 6. When UseShellExecute = false, the code will use the incoming parameter as the filename, throwing a "The system cannot find the file specified" exception.问题原因:因为UseShellExecute在.NET上默认为false 6.当UseShellExecute = false时,代码会使用传入的参数作为文件名,抛出“系统找不到指定的文件”异常。

Reason:原因:

UseShellExecute defaults to false on .NET 6: UseShellExecute 在 .NET 6 上默认为 false:

public bool UseShellExecute { get; set; }

Defaults to true on the .NET Framework:在 .NET 框架上默认为 true:

 [DefaultValue(true)]
 [MonitoringDescription("ProcessUseShellExecute")]
 [NotifyParentProperty(true)]
 public bool UseShellExecute
{
     get
    {
         return this.useShellExecute;
     }
     set
    {
        this.useShellExecute = value;
     }
 }

     private bool useShellExecute = true;

Solution:解决方案:

  1. Set UseShellExecute = true.设置 UseShellExecute = true。
  2. Use .NET Framework to develop projects.使用 .NET 框架开发项目。

Code testing in.Net6: .Net6中的代码测试:

   private void button1_Click(object sender, EventArgs e)
    {
        
        // Define an instance of processstartinfo
        ProcessStartInfo startinfo = new ProcessStartInfo("explore.exe");
        startinfo.FileName = @"C:\test\test.txt";
        startinfo.UseShellExecute = true;
       

        // start the process
      
        try
        {
            Process.Start(startinfo);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
    }

Test Results:试验结果:

在此处输入图像描述

UseShellExecute defaults to false: UseShellExecute 默认为 false:

   private void button1_Click(object sender, EventArgs e)
    {
        
        // Define an instance of processstartinfo
        ProcessStartInfo startinfo = new ProcessStartInfo("explore.exe");
        startinfo.FileName = @"C:\test\test.txt";
       
       

        // start the process
      
        try
        {
            Process.Start(startinfo);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
    }

Test Results:试验结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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