WScript.Shell Run gives "No application is associated with the specified file for this operation." when opening images.
Asked Answered
L

1

7

I am facing an issue while trying to open an image file from a Silverlight app in Windows 10 using WScript.Shell.

The code is as follows.

        try
        {
            dynamic shell = AutomationFactory.CreateObject("WScript.Shell");
            shell.Run(@"C:\temp\X.jpg");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.StackTrace);
        }

This piece of code works perfectly fine when the default application is set to 'Photos' / 'Internet Explorer' in Windows 10 'Default Apps' settings.

However, when the default app is set to 'Paint', I get an exception "No application is associated with the specified file for this operation. (Exception from HRESULT: 0x80070483)"

Please note that when I try to double click on the same image in Windows explorer, it opens up in Paint application without errors.

Why does this happen? Please help.

Lindyline answered 28/10, 2016 at 9:51 Comment(2)
Can this be a x64 vs x86 issue ? Your shell is started using either of those. When referencing IE, it picks the matching version (by luck?) and when referencing Paint, it is the non-matching thus it does not see it.Phinney
try @"start C:\temp\X.jpg" - sorry can't test right nowClaytonclaytonia
N
1

Problem

  • windows script host not running desired application for known file type

Solution

  • expressly specify the desired application and run using WScript.Shell Object

Example

  • in the example below, we reference both the program and the file we want to run with strRunLine

    <?xml version="1.0"?>
    <package>
    <job id="default">
        <script language="jscript">
        <![CDATA[
            // save to demo_runpaint.wsf
            var WshShell        =   new ActiveXObject("Wscript.Shell");
            var strRunLine      =   "";
    
            strRunLine  = "mspaint.exe C:/path/to/capture.png";
            WshShell.Run( strRunLine );
        ]]>
        </script>
    </job>
    </package>
    

Pitfalls

  • the desired program must be in your system PATH or else you must specify the full path to the application expressly.
Nonparous answered 12/4, 2019 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.