物理打开文本文件

Physically opening a text file

我有一个包含文本 "This is a test" 的文本文件 C:\user\test.txt,我想使用 VBScript 物理打开该文件(就像我要单击 附件 → 记事本).我想在我的屏幕上看到打开的文件,以便我可以直观地阅读文本。

现在,出于某种原因我不能。这就是我正在尝试的(我尝试使用和不使用 "textfile.close" 行,是的,该文件存在于该路径中):

dim FS1, textfile

Const ForReading = 1 

set FS1 = CreateObject("Scripting.FileSystemObject")
set textfile = FS1.OpenTextFile("C:\user\test.txt", ForReading, True)
textfile.close

我错过了什么?我在创建、写入或附加时没有问题...但我就是打不开它!

你在读文件吗?

你应该这样做

' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)

' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
    TextLine = MyFile.ReadLine
    Document.Write TextLine & "<br />"
Loop
MyFile.Close

https://msdn.microsoft.com/en-us/library/314cz14s(v=vs.84).aspx

如果要在记事本中打开文件:

With CreateObject("WScript.Shell")
    .Run "notepad.exe c:\user\test.txt"
End With

您也可以只 运行 文件 它将在您的默认 txt 编辑器中打开(无论您有什么 Windows 文件关联建立扩展 txt).

With CreateObject("WScript.Shell")
    .Run "c:\user\test.txt"
End With