Process.Start not working as per examples on internet

126 views Asked by At

I have a problem with opening a file in my .NET 6.0 C# WPF tool.

I'm searching for files in a directory that contains a specific string. Afterwards, the user should be able to open the found files with a button or similar.

The files, I'm finding. My problem is to open those files. I get an error like:

System.ComponentModel.Win32Exception: "An error occurred trying to start process 'Y:/Lehmann/Files\K123456.pdf' with working directory 'C:\Users\OneDrive\Dokumente\IEFileViewe\IEFileViewe\bin\Debug\net6.0-windows'. The specified executable is not a valid application for this OS platform."

This is my code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IEFileViewe
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string KNR;
        string[] files = new string[3];
        string[] allfiles;
        string directory = "Y:/Lehmann/Files";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Überprüfung ob KNR passt
            KNR = txt_Search.Text;
            allfiles = Directory.GetFiles(directory);
            foreach (string file in allfiles) 
            {
                if (file.Contains(KNR) == true)
                {
                    if(file.Contains(".pdf") == true)
                        files[0] = file;
                    else if (file.Contains(".png") == true)
                        files[1] = file;
                }
            }
            for (int i = 0; i < files.Length; i++)
                txtBlock.Text += files[i] + "\n";            
        }

        private void btnPDF_Click(object sender, RoutedEventArgs e)
        {
            //files[0] = files[0].gsub!("\\", "/");
            System.Diagnostics.Process.Start(files[0]);
        }

        private void btnPNG_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(files[1]);
        }
    }
}
1

There are 1 answers

0
Andrew Morton On BEST ANSWER

With .NET Core and later (including the .NET 6 you are using*), you need to set the UseShellExecute parameter to true:

Process.Start(new ProcessStartInfo(files[1]) { UseShellExecute = true });

From the documentation for the ProcessStartInfo.UseShellExecute property:

The default is true on .NET Framework apps and false on .NET Core apps.

So where it was not specified before, when using .NET Framework as in many examples on the internet, it would by default work as you expected.

If it is set to false, then the OS attempts to run it directly as a program; when it is set to true the OS looks for a program to pass the file to. On Windows, that would be by examining the file extension and looking it up in the registry.


* The "Framework" part of the name was dropped when Microsoft went to .NET Core instead of .NET Framework. The "Core" part of the name was dropped when they went to .NET 5 as there would not be any confusion with .NET Framework 4.x—development of that line of software has stopped and there will never be a .NET Framework 5.