FileClose

首页  后退  前进

FileClose
down2

FileClose

关闭先前打开的文件.

 

FileClose ( "filehandle" )

参数

filehandle

由此前 FileOpen() 函数返回的文件句柄.

返回值

成功:

返回 1.

失败:

返回 0, 句柄无效.

备注

AutoIt 退出时会自动关闭打开的文件, 但调用 FileClose 函数关闭文件是一个好主意.

 

本函数可以用来关闭 FileFindFirstFile() 函数返回的搜索句柄.

 

函数示例

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
Example()
Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = _WinAPI_GetTempFileName(@TempDir)
    ; Create a temporary file to read data from.
    If Not FileWrite($sFilePath, "This is an example of using FileClose.") Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
        Return False
    EndIf
    ; Open the file for reading and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
    EndIf
    ; Read the contents of the file using the handle returned by FileOpen.
    Local $sFileRead = FileRead($hFileOpen)
    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)
    ; Display the contents of the file.
    MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead)
    ; Delete the temporary file.
    FileDelete($sFilePath)
EndFunc   ;==>Example

----------------------------------------

 

该函数可以通过命令 exect 调用

参见:

FileOpen

例子
copy

exect=$var_h=FileOpen('c:\Test\1.txt',16)||$var_b=FileRead($var_h,5)||FileClose($var_h) GLOBALEXECT<a> ;; 从文件读取5个字节的示例

 

exect=$var_h=FileOpen('c:\Test\1.txt',1)||FileWrite($var_h,'字符串1'&@CRLF&'字符串2')||FileClose($var_h) GLOBALEXECT<a> ;; 写入多行文本文件末尾的示例

 

exect=$var_h=FileOpen('c:\Test\1.txt',1)||FileWriteLine($var_h,'字符串1')||FileClose($var_h) ;; 写入文件末尾的例子

 

exect=$var_sp='c:\Test\1.txt'||$var_st=FileRead('$var_sp')||$var_h=FileOpen('$var_sp',2)||FileWrite($var_h,'字符串1'&'$var_st')||FileClose($var_h) GLOBALEXECT<a> ;; 写入文件开头的示例

 

exect=$var_h=FileOpen('c:\Test\1.txt')||FileReadLine($var_h,5)||FileClose($var_h) GLOBALEXECT<a> ;; 从文本文件读取第5行的示例

 

exect=$var_h=FileFindFirstFile('C:\Test\*.*')||_ViewValues('$var_h')||$var_sfile=FileFindNextFile($var_h)||_ViewValues('$var_sfile')||FileClose($var_h) ;;在测试目录中找到第一个文件/文件夹名称的示例

 

exect=$var_h=FileOpen('%P%N',16)||$var_s=BinaryToString(FileRead($var_h),4)||FileClose($var_h)||_ViewValues('$var_s') ;;以UTF-8编码读取光标下的文件数据的示例

up2

tcimage © Аверин Андрей для Total Commander Image Averin-And@yandex.ru