Skip to content
⌘ NSIS Forum Archive

Write a line

9 posts

Gluck#

Write a line

I want write a line of text (in portable mode) in hosts file...

What have I to write?

Thanks
Anders#
Have you tried searching the forum and wiki?

Portable applications and host files don't really mix since you have to be elevated to change the hosts file and you don't really want to elevate the portable application.
Gluck#
"... since you have to be elevated to change the hosts file and you don't really want to elevate the portable application".

What does it mean???

And If I want elevate the portable application?
Anders#
Originally Posted by Gluck View Post

And If I want elevate the portable application?
You just asked about Drag & Drop in another thread and you cannot elevate if you want to be a drag target.

Elevation is not really relevant to the topic at hand, this is about text file manipulation.
Gluck#
I have written:

WindowIcon Off
SilentInstall Silent
AutoCloseWindow True
OutFile Hosts.exe

Section

FileOpen $4 "$SYSDIR\drivers\etc\hosts" a
FileSeek $4 0 END
FileWrite $4 "$\r$\n" ; we write a new line
FileWrite $4 "144.144.144.144"
FileClose $4 ; and close the file

ExecWait "$EXEDIR\App\notepad.exe"

FileOpen $4 "$SYSDIR\drivers\etc\hosts" w
loop:
FileRead $0 $2
IfErrors done
StrCmp $2 "144.144.144$\r$\n" 0 +2
StrCpy $2 "$\r$\n"
FileWrite $1 $2
Goto loop

done:
FileClose $0

SectionEnd

The part BEFORE execution works fine (add a line at the end of hosts file).
The part AFTER execution delete hosts leaving the file empty...

What's wrong?

Thanks.

What is the elevation term? Is like acquire admin privileges for the program?
Gluck#
I have used a, but the IP remain.
I have used r, and I have found the IP duplicate...

What have I to write?
The code is optimized?
jooseng#
I use Portableapps.com launcher generator to make my portable app. This is what I do in my portable app.

To block a url:

CopyFiles /Silent "$SYSDIR\drivers\etc\hosts" "$SYSDIR\drivers\etc\hosts.BackupByxxxxPortable"
SetFileAttributes `$SYSDIR\drivers\etc\hosts` NORMAL
nsExec::Exec `"$SYSDIR\cmd.exe" /c @echo: >> "$SYSDIR\drivers\etc\hosts"`
nsExec::Exec `"$SYSDIR\cmd.exe" /c echo 127.0.0.1 www.xxxx.com >> "$SYSDIR\drivers\etc\hosts"`
SetFileAttributes `$SYSDIR\drivers\etc\hosts` READONLY

To restore the orginal hosts file:

${If} ${FileExists} "$SYSDIR\drivers\etc\hosts.BackupByxxxxPortable"
Delete "$SYSDIR\drivers\etc\hosts"
Rename "$SYSDIR\drivers\etc\hosts.BackupByxxxxPortable" "$SYSDIR\drivers\etc\hosts"
${EndIf}