-1

I'm working on a WPF application that use Process.Start() method to open images and PDF files with default viewer app.

What's the problem?

Every time I call Process.Start() method the "Open With" dialog shows up even if I check the "Always use this app to open .jpg files" option.

This is my C# code:

public void Open(string fileName, bool isPdf)
{
    var sharedFolderPath = AppCore.Settings.SharedFolder.Path;
    string message;

    if (sharedFolderPath is null)
    {
        message = Resources.EmptySharedFolderErrorMessage;
        AppCore.ShowDialog(AppCore.GetMessageDialog(message));
        return;
    }

    var path = Path.Combine(sharedFolderPath, fileName);

    if (File.Exists(path))
    {
        try
        {
            if (isPdf || AppCore.Settings.ImageViewerSettings.OpenInWindowsDefaultApp)
            {
                Process.Start(path);
            }
            else
            {
                var imageViewerWindow = new ImageViewerWindow(path);
                imageViewerWindow.ShowDialog();
            }
        }
        catch (Exception ex)
        {
            AppCore.Logger.Info("Opening shared file failed.");
            AppCore.Logger.Exception(ex);
        }

        return;
    }

    message = string.Format(Resources.SharedFolderFileNotFoundMessage, fileName);
    AppCore.ShowDialog(AppCore.GetMessageDialog(message));
}

My problem with more details: https://i.stack.imgur.com/FKWye.jpg

EDITED

I created a simple WPF project and tried to open the image with Process.Start() method. In the new project the image was opened with the default app without showing "Open With" dialog.

AmRo
  • 833
  • 1
  • 8
  • 19
  • How about trying to detect what is the current default application to open the file type (https://stackoverflow.com/a/162351/4329813)? Then pass the default application path to Process.FileName – popsiporkkanaa Sep 25 '20 at 07:54
  • Looks like there is no default application registered for the filesystem type you are trying to open. – BionicCode Sep 25 '20 at 07:55
  • This is not a problem of the API. This is the default behaviour of Process.Start. Something that the user needs to fix. – BionicCode Sep 25 '20 at 07:57
  • @BionicCode How can I fix this issue? – AmRo Sep 25 '20 at 08:01
  • In Windows default apps settings Windows 10 Photos app registered for Photo viewer. – AmRo Sep 25 '20 at 08:03
  • @popsiporkkanaa Nice idea. If I couldn't fix this issue, I'll try that solution. – AmRo Sep 25 '20 at 08:09
  • Then verify that the path and the extension of the file is correct. Does it work with other files of same type and different type? – BionicCode Sep 25 '20 at 08:10
  • I did before, everything looks ok. – AmRo Sep 25 '20 at 08:14
  • Hard to believe. This is a simple method that takes a single parameter as input. So it's very likely that the issue is related to this input or the Windows environment. Opening this file using Explorer works? Copying the path and hard code this file opening works? If you just set the default application, tried to reboot the machine? Reboot machine? Clean and rebuild solution. Copy the combined path from your application and paste it into Explorer, does the file open? Can you open the file using ProcessInfo? Then try to specify the target application explicitly, does this work? – BionicCode Sep 25 '20 at 08:49
  • Opening this file using Explorer works? Works everywhere except in my WPF app | If you just set the default application, tried to reboot the machine? Reboot machine? I tried | Copy the combined path from your application and paste it into Explorer, does the file open? Yes, it opens the file – AmRo Sep 25 '20 at 09:03
  • I'll try in other systems and put the result here. – AmRo Sep 25 '20 at 09:07
  • Have you tried to execute the application as administrator, does it work? (worth a try) – BionicCode Sep 25 '20 at 09:14
  • I tried now. Same result – AmRo Sep 25 '20 at 10:01
  • Did you try to set `UseShellExecute` to `true`? – Pavel Anikhouski Sep 25 '20 at 10:04
  • Also try to execute process.start. from a new blank solution. Just from app.xaml.cs. if this should work you can try to figure out the differences in e.g. Project configuration. – BionicCode Sep 25 '20 at 10:48
  • As I mentioned in the last part of my question. `Process.Start()` Method works fine in other project. – AmRo Sep 25 '20 at 12:38
  • I got new information about the problem. This issue only occurs for JPG files and only in .net framework (my app framework version 4.7.2). I created a .net core app and everything works like I expected in .net core. – AmRo Sep 25 '20 at 12:56
  • It's better to use .NET Core instead of .NET Framework for new projects. .NET Framework is discontinued. New features like `IAsyncEnumerable` will be available in .NET Core (or the new consolidated .NET 5.0) only. – BionicCode Sep 25 '20 at 14:02
  • Definitely .net core is better, but in this case some users still using Windows 7 that doesn't support .net core (according to this post: https://learn.microsoft.com/en-us/dotnet/core/install/windows?tabs=netcore31#additional-deps). – AmRo Sep 25 '20 at 15:11
  • It's supported for Windows 7 SP1 (see [Dependencies](https://learn.microsoft.com/en-us/dotnet/core/install/windows?tabs=netcore31#dependencies)) – BionicCode Sep 25 '20 at 15:49

1 Answers1

0

I don't know why/how, but my problem solved after uninstalling TechSmith Snagit that I installed yesterday.

AmRo
  • 833
  • 1
  • 8
  • 19