【发布时间】:2018-08-25 21:27:23
【问题描述】:
我最近为我的 Java 软件使用了 Inno Setup。我正在编写一个函数来通过调用wmic printer get name /All 并读取它的输出来检查打印机驱动程序是否存在。但问题是当我读取文本文件并检查它是否包含Pos() 的特定子字符串时,它总是返回 0,但是当我尝试使用字符进行测试时,它返回了真值。我目前使用的是 5.6.1 版 Unicode。
我看过Delphi Pos always returning 0,但我认为这不是我的情况:
我是这样做的:
function isContainedInFile(File, Substring: String): Boolean;
var
Lines: TArrayOfString;
i: Integer;
line: String;
begin
Substring := Uppercase(Substring);
Result := False;
if LoadStringsFromFile(File, Lines) then
begin
for i:= 0 to GetArrayLength(Lines) - 1 do
begin
line := Lines[i];
if (Length(line) = 0) then
continue;
line := Uppercase(Trim(line));
Log('Substring:' + Substring + ', Line:' + line + ', Pos:' + IntToStr(Pos(Substring, line)));
if (Pos(Substring, line) <> 0) then
begin
Result:= True;
break;
end;
end;
end;
end;
我就是这样称呼isContainedInFile():
function IsBrotherDriverInstalled(): Boolean;
var
path, brotherPath, ListPrinterPath, ListPrinter: String;
check, index: Integer;
begin
ListPrinterPath := ExpandConstant('{tmp}\printerlist.tdm');
{ Save temporarily the list }
Exec(ExpandConstant('{cmd}'), '/c wmic printer get name /All > "' + ListPrinterPath + '"',
'', SW_HIDE, ewWaitUntilTerminated, check);
{ Check if the list has the printer }
Result := isContainedInFile(ListPrinterPath, PrinterName);
{ Delete the file }
DeleteFile(ListPrinterPath);
end;
这是子字符串长度 > 1 时的输出:
当子串的长度=1时:
提前致谢。
【问题讨论】: