默认情况下,将hta设置为以记事本形式打开时,CreateTextFile不起作用(使用JavaScript) [英] CreateTextFile not working when hta is set to open as notepad by default (javascripting)

查看:85
本文介绍了默认情况下,将hta设置为以记事本形式打开时,CreateTextFile不起作用(使用JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用javascript和hta文件创建文本文件时,我遇到了这个非常奇怪的问题.

I'm getting this really weird problem when I try to create a text file using javascripting and a hta file.

这是细分为基本代码的代码:

This is the code broken down to its basics:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>

alert("creating file");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("test.txt", true);
s.WriteLine("it works");
s.Close();
alert("file created");
</script>
</head>

<body>
</body>
</html>

这在一个名为"Untitled.hta"的hta文件中 只要将打开方式">选择默认程序"设置为"Microsoft(R)HTML应用程序主机",然后打开hta文件,就可以创建文本文件.

This is in a hta file called "Untitled.hta" As long as I set the Open With > Choose Default Program to "Microsoft (R) HTML Application Host" and then open the hta file the text file gets created just fine.

但是,如果我将打开方式"设置为记事本",然后选择默认程序",然后使用"Microsoft(R)HTML Application Host"打开,则不会创建该文本文件.

But if I set Open With > Choose Default Program to "Notepad" and then Open With "Microsoft (R) HTML Application Host" the text file doesn't get created.

有人知道为什么会这样吗? 通常没关系,但是如果客户端默认将hta文件设置为以记事本打开,那么我的hta文件将无法按预期工作.

Does anyone know why this is happening? It normally wouldn't matter but if a client has hta files set to open as notepad by default then my hta file wont work as intended.

使事情变得更加复杂

var s = fso.CreateTextFile("test.txt", true);

使用

var s = fso.CreateTextFile("TestFolder/test.txt", true);

hta文件所在的位置创建一个名为"TestFolder"的文件夹

and where the hta file is located create a folder called "TestFolder"

如果您执行的操作与以前相同,则将其默认设置为记事本"会出现找不到路径"错误(但如果默认设置为Microsoft(R)HTML Application Host,则可以正常工作)

If you do the same thing as you did before, having it set to Notepad as default gives the Path not found error (but it works fine if the default is set to Microsoft (R) HTML Application Host)

推荐答案

发生这种情况是因为您使用的是相对路径.在创建FSO之前,请先添加以下代码段:

That happens because you are using a relative path. Put this snippet before creating a FSO:

var shell = new ActiveXObject("WScript.Shell");
alert(shell.currentDirectory);

可能是您的HTA警报C:\Windows\System32(取决于所使用的操作系统).这是文件夹,您可以在其中找到test.txt.现在还解释"Path not found"错误...

Probably your HTA alerts C:\Windows\System32 (depended on the used OS). This is the folder, where you can find test.txt. Also "Path not found" error is now explained...

要解决此问题,请仅使用绝对路径,或设置当前目录:

To fix the issue, use absolute paths only, or set the current directory:

shell.currentDirectory = 'C:/Some_Path';

这篇关于默认情况下,将hta设置为以记事本形式打开时,CreateTextFile不起作用(使用JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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