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

I encountered a problem while trying to open an image file from a Silverlight application 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 code snippet works great when the application is set to "Photos" / "Internet Explorer" in the Windows 10 "Default Settings" by default.

However, when the application is set to "Paint" by default, I get the 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 the same image in Windows Explorer, it opens in the Paint application without errors.

Why is this happening? Please help.

+6
source share
1 answer

problem

  • The host application for the known file type is not running on the Windows script host

Decision

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

example

  • in the example below, we strRunLine both on the program and on the file that we want to run using 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> 

Traps

  • the desired program must be in your system variable PATH, otherwise you must specify the full path to the application.
0
source

Source: https://habr.com/ru/post/1011807/


All Articles