无法在文件中查找字符串并使用 Inno Setup 填充它?
Unable to Find String in a file and populate it using Inno Setup?
我有 demo.properties
文件,我可以加载文件并迭代以获取其中存在的所有值。
hibernate.connection.username=jack
hibernate.connection.password=queen
hibernate.connection.url=jdbc:jtds:sqlserver://localhost/cabinet
但是当我得到第 1 行时(能够逐行获取但无法获取特定字符串)并且我想填充 jack 并将其存储到用户名字符串中,类似地 queen 存储到密码字符串中,localhost 存储到数据库中 String.This是我的 code/logic 获取值。
procedure InitializeWizard;
var
xmlInhalt: TArrayOfString;
k : Integer;
CurrentLine : String;
Uname : String;
Password : String;
HostName : String;
STR : String;
begin
LoadStringsFromFile('C:\demo.properties', xmlInhalt);
for k:=0 to GetArrayLength(xmlInhalt)<>-1 do
begin
CurrentLine := xmlInhalt[k];
MsgBox(CurrentLine, mbError, MB_OK);
if (Pos('hibernate.connection.username=', CurrentLine) <>-1 ) then
begin
MsgBox(CurrentLine, mbError, MB_OK);
Uname := Pos('://', CurrentLine);
STR :=IntToStr(Uname);
STR :=IntToStr(Length('://'));
Password := Pos(':1', CurrentLine);
HostName :=Password -Uname;
end;
end;
end;
请帮助我获得 requirement.Your 帮助将不胜感激。
如果 TStrings
class 发布了 NameValueSeparator
和 Values
属性,我建议使用它。但它没有,所以这里有一个解决方法的代码(它使用 TArrayOfString
,但很容易为 TStrings
class 修改它):
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
function TryGetValue(const Strings: TArrayOfString; const Name: string;
out Value: string): Boolean;
var
S: string;
P: Integer;
I: Integer;
begin
Result := False;
{ loop through the array }
for I := 0 to GetArrayLength(Strings) - 1 do
begin
{ store the currently iterated string into a local variable }
S := Strings[I];
{ try to get position of the name value separator ('='in this case) }
P := Pos('=', S);
{ if the separator was found on this line, and a text on the left of }
{ it matches (case insensitively) the input Name parameter value, we }
{ found what we were looking for, so return True and the rest of the }
{ text after the found separator }
if (P <> 0) and (CompareText(Copy(S, 1, P - 1), Name) = 0) then
begin
Value := Copy(S, P + 1, MaxInt);
Result := True;
Exit;
end;
end;
end;
{ do note, that this function may not conform the RFC 3986 specification; }
{ preferred way should be e.g. InternetCrackUrl, but with this particular }
{ scheme (jdbc:jtds:sqlserver) it didn't crack the URL properly }
function GetHostName(const URL: string): string;
var
P: Integer;
begin
Result := '';
P := Pos('://', URL);
if P <> 0 then
begin
Result := Copy(URL, P + 3, MaxInt);
P := Pos('/', Result);
if P = 0 then
P := MaxInt;
Result := Copy(Result, 1, P - 1);
end;
end;
procedure InitializeWizard;
var
URL: string;
HostName: string;
UserName: string;
Password: string;
StrArray: TArrayOfString;
begin
if LoadStringsFromFile('C:\File.txt', StrArray) then
begin
TryGetValue(StrArray, 'hibernate.connection.url', URL);
HostName := GetHostName(URL);
TryGetValue(StrArray, 'hibernate.connection.username', UserName);
TryGetValue(StrArray, 'hibernate.connection.password', Password);
MsgBox(Format(
'HostName: %s' + #13#10 + 'UserName: %s' + #13#10 + 'Password: %s', [
HostName, UserName, Password]
), mbInformation, MB_OK);
end;
end;
我有 demo.properties
文件,我可以加载文件并迭代以获取其中存在的所有值。
hibernate.connection.username=jack
hibernate.connection.password=queen
hibernate.connection.url=jdbc:jtds:sqlserver://localhost/cabinet
但是当我得到第 1 行时(能够逐行获取但无法获取特定字符串)并且我想填充 jack 并将其存储到用户名字符串中,类似地 queen 存储到密码字符串中,localhost 存储到数据库中 String.This是我的 code/logic 获取值。
procedure InitializeWizard;
var
xmlInhalt: TArrayOfString;
k : Integer;
CurrentLine : String;
Uname : String;
Password : String;
HostName : String;
STR : String;
begin
LoadStringsFromFile('C:\demo.properties', xmlInhalt);
for k:=0 to GetArrayLength(xmlInhalt)<>-1 do
begin
CurrentLine := xmlInhalt[k];
MsgBox(CurrentLine, mbError, MB_OK);
if (Pos('hibernate.connection.username=', CurrentLine) <>-1 ) then
begin
MsgBox(CurrentLine, mbError, MB_OK);
Uname := Pos('://', CurrentLine);
STR :=IntToStr(Uname);
STR :=IntToStr(Length('://'));
Password := Pos(':1', CurrentLine);
HostName :=Password -Uname;
end;
end;
end;
请帮助我获得 requirement.Your 帮助将不胜感激。
如果 TStrings
class 发布了 NameValueSeparator
和 Values
属性,我建议使用它。但它没有,所以这里有一个解决方法的代码(它使用 TArrayOfString
,但很容易为 TStrings
class 修改它):
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
function TryGetValue(const Strings: TArrayOfString; const Name: string;
out Value: string): Boolean;
var
S: string;
P: Integer;
I: Integer;
begin
Result := False;
{ loop through the array }
for I := 0 to GetArrayLength(Strings) - 1 do
begin
{ store the currently iterated string into a local variable }
S := Strings[I];
{ try to get position of the name value separator ('='in this case) }
P := Pos('=', S);
{ if the separator was found on this line, and a text on the left of }
{ it matches (case insensitively) the input Name parameter value, we }
{ found what we were looking for, so return True and the rest of the }
{ text after the found separator }
if (P <> 0) and (CompareText(Copy(S, 1, P - 1), Name) = 0) then
begin
Value := Copy(S, P + 1, MaxInt);
Result := True;
Exit;
end;
end;
end;
{ do note, that this function may not conform the RFC 3986 specification; }
{ preferred way should be e.g. InternetCrackUrl, but with this particular }
{ scheme (jdbc:jtds:sqlserver) it didn't crack the URL properly }
function GetHostName(const URL: string): string;
var
P: Integer;
begin
Result := '';
P := Pos('://', URL);
if P <> 0 then
begin
Result := Copy(URL, P + 3, MaxInt);
P := Pos('/', Result);
if P = 0 then
P := MaxInt;
Result := Copy(Result, 1, P - 1);
end;
end;
procedure InitializeWizard;
var
URL: string;
HostName: string;
UserName: string;
Password: string;
StrArray: TArrayOfString;
begin
if LoadStringsFromFile('C:\File.txt', StrArray) then
begin
TryGetValue(StrArray, 'hibernate.connection.url', URL);
HostName := GetHostName(URL);
TryGetValue(StrArray, 'hibernate.connection.username', UserName);
TryGetValue(StrArray, 'hibernate.connection.password', Password);
MsgBox(Format(
'HostName: %s' + #13#10 + 'UserName: %s' + #13#10 + 'Password: %s', [
HostName, UserName, Password]
), mbInformation, MB_OK);
end;
end;