nsis从txt文件读取目录吗?

时间:2019-01-06 03:25:32

标签: nsis

我正在尝试从文本文件中读取目录,但是它不起作用!

test.txt是一个两行文件,每个目录一行。

有人可以帮助我吗?

; example1.nsi
;
; This script is perhaps one of the simplest NSIs you can make. All of the
; optional settings are left to their default settings. The installer simply 
; prompts the user asking them where to install, and drops a copy of example1.nsi
; there. 

;--------------------------------

; The name of the installer
Name "Example1"

; The file to write
OutFile "example1.exe"

; The default installation directory
InstallDir $DESKTOP

; Request application privileges for Windows Vista
RequestExecutionLevel admin
!include "LogicLib.nsh"
!include FileFunc.nsh
ShowInstDetails show


;--------------------------------

; Pages


;--------------------------------


Function .onInit



functionend

; The stuff to install
Section "" ;No components page, name is not important
FileOpen $0 "test.txt" r
;FileSeek $4 1000 ; we want to start reading at the 1000th byte
FileRead $0 $1 ; we read until the end of line (including carriage return and new line) and save it to $1
FileRead $0 $2
;FileRead $4 $2 10 ; read 10 characters from the next line
FileClose $0 ; and close the file
DetailPrint $1
DetailPrint $2


CopyFiles $1 "D:\Desktop\taobao"
  ; Set output path to the installation directory.
SetOutPath $INSTDIR


SectionEnd ; end the section

1 个答案:

答案 0 :(得分:1)

FileOpen $0 "test.txt" r有问题的原因有两个。

  1. 您未使用完整路径。
  2. 我假设此文件在最终用户计算机上尚不存在。您可能需要从安装程序中提取它,然后才能阅读它。

另一个问题是FileRead在返回的字符串中包含换行符,如果不需要,必须将其删除。换行符在Windows的路径中无效。

; Create simple text file for this example:
!delfile /nonfatal "myexample.txt"
!appendfile "myexample.txt" "Hello$\r$\n"
!appendfile "myexample.txt" "W o r l d$\r$\n"

!include "StrFunc.nsh"
${StrTrimNewLines} ; Tell StrFunc.nsh to define this function for us

Section
SetOutPath $InstDir

InitPluginsDir ; Make sure $PluginsDir exists (it is automatically deleted by the installer when it quits)
File "/oname=$PluginsDir\data.txt" "myexample.txt" ; Extract our myexample.txt file to $PluginsDir\data.txt

FileOpen $0 "$PluginsDir\data.txt" r
FileRead $0 $1
${StrTrimNewLines} $1 $1
FileRead $0 $2
${StrTrimNewLines} $2 $2
FileClose $0
DetailPrint "Line 1=$1"
DetailPrint "Line 2=$2"
SectionEnd