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