【问题标题】:wsh currentdirectory broken on Win 8.1?wsh 当前目录在 Win 8.1 上损坏?
【发布时间】:2013-11-08 20:44:47
【问题描述】:

我们使用 HTA 文件来构建安装 CD,以便我们可以显示一些介绍性信息和指向不同安装选项的链接 - 每个链接都指向相应的 setup.exe 等文件。这在 Windows 7 之前运行良好,但在 8.1(-未尝试 8.0)上它失败并出现错误“找不到指定的文件”。原来这是因为当前目录现在是 C:\Windows\System32,而它曾经是文件所在的目录 - 在 CD 驱动器上,因此 相对路径可以工作 CD 上其他文件的链接;现在他们没有了。

换句话说,在 hta 文件中使用此代码:

<script type="text/javascript" language="javascript">
    function RunFile(appname) 
    {
    WshShell = new ActiveXObject("WScript.Shell");
    alert( WshShell.CurrentDirectory );
    WshShell.Run(appname, 1, false);
    }

在 win 8.1 上,我们在警告框中看到 C:\Windows\System32,因此 ..\ourproduct\setup.exe 等相对路径不再起作用。

这是一个错误吗?有什么想法可以解决这个问题吗?

【问题讨论】:

  • 您可以从window.location.pathname 中提取正确的路径,然后将CurrentDirectory 属性设置为正确的值。
  • @Teemu 谢谢,这正是我所需要的;使用一些 lastIndexOf 和 Slice 从末尾删除文件名,它可以完美地工作。如果您将评论变成答案,我会接受。

标签: hta wsh


【解决方案1】:

您可以从window.location.pathname 中提取正确的路径,然后将值设置为CurrentDirectory。我使用了类似下面的代码:

var shell = new ActiveXObject('WScript.Shell'),
    defaultInstallationFolder = 'installation_folder_name',
    currentPath = window.location.pathname.replace(/\\/g,'/'),
    defaultRootPath;
if (currentPath.charAt(0) === '/')  { // For the browser environment
    currentPath = currentPath.substring(1, currentPath.length);
}
currentPath = currentPath.split(defaultInstallationFolder);
defaultRootPath = currentPath[0] + defaultInstallationFolder;
shell.CurrentDirectory = defaultRootPath;

IE 和 HTA 给出的 pathname 略有不同(在 IE 中它以 / 开头)。有时在 IE 中调试 HTA 很不错,因此检查 currentPath

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-20
    • 2014-07-15
    • 2021-04-29
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多