Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 756
Chhavi Sehgal Avatar asked Oct 28 '16 09:10

Chhavi Sehgal


People also ask

What is the use of Wscript shell?

If you do not specify a script or any script arguments, wscript.exe displays the Windows Script Host Settings dialog box, which you can use to set global scripting properties for all scripts that wscript.exe runs on the local computer. The /t parameter prevents excessive running of scripts by setting a timer.

What is CreateObject Wscript shell?

CreateObject instantiates an instance of an outside application into the VBscript runtime. Basically, in the case of wscript. shell, it creates an object that is an instance of the windows shell allowing you to run commands against the windows shell from within your VBScript.

What is Wshshell run?

The Run method creates a new process and runs the command specified by strCommand. The optional parameter intWindowStyle is used to set the window style of the program being run. If the optional parameter bWaitOnReturn is set to True (default is False), then Run will return the return value returned by strCommand.


1 Answers

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.
like image 169
dreftymac Avatar answered Oct 22 '22 15:10

dreftymac