使用 HTA 应用程序打开文件 [英] Open a file with an HTA application

查看:53
本文介绍了使用 HTA 应用程序打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 HTA 文件的代码:

Here's a code for an HTA file:

<html>
<head>
     <title>HTA application</title>
     <script type="text/javascript">
          function f(x){alert("You opened the file " + x)}
     </script>
</head>
<body>
     Here's some text in an HTA application...
</body>
</html>

是否有办法将文件扩展名与 Windows 上的此 HTA 应用程序相关联,以便当您打开具有此扩展名的文件时,它会打开 HTA 应用程序并执行函数 f(path) 其中 path 是打开文件的路径吗?

Is there a way of associating a file extension with this HTA application on Windows so that when you open a file with this extension it opens the HTA application and does the function f(path) where path is the path of the opened file?

推荐答案

您可以使用 GET 参数将参数传递给 HTA 文件,例如文件名.为此,您可以在 HTA 文件的路径后添加一个 ?,然后添加您想要传递的任何参数,例如 C:\path\program.hta?parameter.

You can use GET parameters to pass parameters to an HTA file, for example a file name. To do that, you add a ? after the HTA file's path, and then add whatever parameters you want to pass, for example C:\path\program.hta?parameter.

为了将某种文件类型与 HTA 程序相关联,您可以将文件扩展名就像您对可执行文件所做的那样,除了不是用 someprogram.exe "%1" 打开它,你会用 mshta.exe "C:\path\program.hta?%1" 打开它.当您打开文件时,%1 部分将自动替换为正在打开的文件的名称.

In order to associate a certain file type with an HTA program, you can associate the file extension just like you would do for an executable, except that instead of opening it with someprogram.exe "%1" you would open it with mshta.exe "C:\path\program.hta?%1". When you open the file, the %1 part will automatically get replaced with the name of the file that's being opened.

以下是如何将文件扩展名为 .test 的文件关联到位于 C:\path\program.hta 的 HTA 文件:

Here is how you would associate files with the file extension .test to an HTA file at C:\path\program.hta:

HKEY_CLASSES_ROOT\.test
    examplefile
HKEY_CLASSES_ROOT\examplefile\shell\open\command
    mshta.exe "C:\path\program.hta?%1"

为了在 HTA 程序中使用 Javascript 访问这些参数,location.search.substr(1) 返回问号之后的所有内容.因此,在传递的唯一参数是文件名的简单情况下,例如:

In order to access these parameters in the HTA program using Javascript, location.search.substr(1) returns everything that's after the question mark. So in a simple case where the only parameter that's being passed is the file name, you would have for example:

<html>
<head>
     <title>HTA application</title>
     <script type="text/javascript">
          alert("You opened the file " + location.search.substr(1))
     </script>
</head>
<body>
     Here's some text in an HTA application...
</body>
</html>

您也可以通过在参数字符串中使用分隔符来传递其他参数.在 Web 上(与 HTA 程序相反),最常见的分隔符是 &,例如 C:\path\program.hta?parameter1&parameter2.如果您的参数是文件名,则使用 & 的问题在于文件名可以包含 & 字符.在网络上,这通常是通过用 %26 转义参数名称中的任何 & 字符来解决的,但我不知道有什么方法可以转义文件名,例如这在注册表中,所以你需要小心这一点.对于可以将文件名作为参数的 HTA 文件,我建议使用另一个不能在文件名中使用的分隔符,例如 *|.在网络上,坚持约定并使用 & 作为分隔符的主要原因是因为像 PHP 这样的服务器端编程语言将 & 识别为分隔符并提供方法可以轻松访问由 & 分隔的参数,但是 HTA 程序没有服务器,因此只要您使用 HTA,您就不必担心服务器端语言会做什么.

You can pass other parameters as well by using delimiters in the parameter string. On the web (as opposed to in HTA programs), the most common delimiter is &, for example C:\path\program.hta?parameter1&parameter2. The problem with using & if your parameters are filenames is that filenames can contain an & character. On the web, this is usually solved by escaping any & characters in the parameter names by %26, but I'm not aware of any way to escape file names like this in the registry, so you need to be careful about this. For HTA files that can take file names as parameters I would recommend using another delimiter that can't be used in file names, for example * or |. On the web, the main reason to stick to the convention and use & as a delimiter is because server side programming languages like PHP recognize & as a delimiter and provide ways to access parameters delimited by & easily, but HTA programs don't have servers so as long as you're using HTA you don't need to worry about what server side languages do.

这篇关于使用 HTA 应用程序打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆