-2

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]);
        }
    }
}
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • The problem is _not_ about double backslashes, that's the inspector window showing you the escaped string. XY problem. Explain what the actual error is. – CodeCaster Aug 23 '23 at 16:03
  • 1
    Do you have a pdf reader installed on your computer? Can you double click the file in explorer and open it? – Poul Bak Aug 23 '23 at 16:14
  • 3
    Windows has no problem at all with the mixing and matching of forwards/backwards slashes, `System.Diagnostics.Process.Start(@"C:/some\path/to\something");` shouldn't be a problem at all. – Nick is tired Aug 23 '23 at 16:15
  • @PoulBak of course i can open it manually – user20202784 Aug 23 '23 at 16:18
  • @Nickistired yes normally, but for me it's not working – user20202784 Aug 23 '23 at 16:19
  • 1
    Try: `System.Diagnostics.Process.Start(new ProcessStartInfo(files[1]) { UseShellExecute = true });` – Poul Bak Aug 23 '23 at 16:28
  • @user20202784 Which program does it open in when you open it manually? – Andrew Morton Aug 23 '23 at 16:31
  • @AndrewMorton the PDFxChange Editor – user20202784 Aug 23 '23 at 17:25
  • @PoulBak Maybe do you know also why that worked. So to learn why that error was coming – user20202784 Aug 23 '23 at 17:30
  • 1
    @user20202784 The default value for [ProcessStartInfo.UseShellExecute](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.useshellexecute) changed from True to False from .NET Framework to .NET [Core]. It makes the operating system look for the program to open the file with instead of trying to open the file without an app. – Andrew Morton Aug 23 '23 at 17:39
  • 1
    @user20202784 I edited the question to add the error message, which is useful information, and remove the supposition that the slashes/backslashes were the problem. I tried to make the title more relevant too. – Andrew Morton Aug 23 '23 at 18:17
  • @user20202784 What version of .NET are you using? – TylerH Aug 23 '23 at 18:29
  • yes I'm using .Net 6 Framework – user20202784 Aug 23 '23 at 19:58
  • 5
    @user20202784 the "Framework" part of the name was dropped when MS went to .NET Core instead of .NET Framework. The "Core" part of the name was dropped when they went to .NET 5. So, you are using .NET 6. Just to keep the names straight ;) – Andrew Morton Aug 23 '23 at 20:50

1 Answers1

1

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.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84