Windows Gadget開發之運行外部程式

來源:互聯網
上載者:User

在Windows側邊欄工具開發時,想做一個和Windows一樣的功能,基本上是以下兩個功能:

1. 可以直接運行系統Path環境變數中列出路徑下的程式

2. 可以直接運行UNC地址,URL網址和FTP地址,即支援常見的網路通訊協定訪問方式

其實gadget開發API中已有函數支援此功能的函數,只不過我開始選錯了,我開始用的是

var winShell = new ActiveXObject("Shell.Application");
winShell.ShellExecute("\""+q+"\"");
winShell = null;

 

發現該方法只能運行程式,但是不支援參數傳遞,幸好有找到個替代產品,System.Shell.execute(appPath, null, null, open);

函數原型定義為:

execute Method

Launches an application.

Syntax

 
System.Shell.execute(strFile [, strArgs] [, strDir] [, strOperation])

Parameters

strFile

Required. String that specifies a Universal Naming Convention (UNC) path to an executable file, a filename, or a URL.

strArgs

Optional. If strFile specifies an executable file then String specifies the parameters to be passed to the application. For example, a UNC path such as System.Gadget.path + "\\MyFile.Txt".

Note  If strFile specifies a filename or URL, strArgs is unneccessary and should be blank, null, or an empty string.

strDir

Optional. String that specifies the UNC path for the default (working) directory of the executable file.

strOperation

Optional. String that specifies the action (or Microsoft Windows Shell verb) to be performed. The set of available verbs depends on the particular file or folder.

 

edit
Opens the file specified by strArgs for editing.
explore
Opens the folder specified by strDir for exploring.
find
Opens the folder specified by strDir for searching.
open
Opens the file specified by strArgs.
print

 

Prints the file specified by strArgs on the default printer.

Return Value

No return value.

Remarks

Supplying a path to a filename (with extension) or a URL for strFile will launch the default application associated with that file extension and load the filename specified.

Note  Alternatively, a path to a known application .exe can by supplied for strFile while strArgs, strDir, and strOperation are used to specify the rest of the file information.

strArgs, strDir, and strOperation can be assigned values in combination as required.

UNC paths should escape special characters with a '\'.

System.Shell.execute第一個參數是應用程式的路徑,第二個參數是參數列表,第三個參數是應用程式工作目錄,第四個是開啟檔案。

用上這個函數實現運行欄的功能還需要做一個命令列的解析工作,由於gadget工作在頁面中,用javascript進行解析需要Regex來識別應用程式路徑和參數列表,代碼如下:

function retApp(str, retPart){
   var s = str;
   var re = new RegExp("[ \f\n\r\t\v]*(\".+\")|([^ \f\n\r\t\v]+)","ig");
   
   var arr = re.exec(s);
  if (retPart == 0) {
   return(RegExp.lastMatch);
  } else {
   return(RegExp.rightContext);
  }
  
   return("");
}

System.Shell.execute(retApp(q, 0), retApp(q, 1), null, open);

現在命令列解析有了,執行命令也有了,就可以接收 command.exe  args[] 這樣的輸入格式了,Regex將第一個出現的邊界類字串作為命令或程式路徑的結束位置,後面所以的字串作為命令的參數。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.