
摘要:对于想要修改 Hosts 文件的朋友来说我们除了自己手动定位到 Hosts 文件使用编辑器编辑外我们还可以通过批处理命令进行编辑。今天大眼仔旭要给大家介绍的是通过使用 Inno Setup 安装包在安装程序的时候对 Hosts 进行修改以达到屏蔽某些应用程序访问某些服务器进行验证的效果。 Windows 主机文件存储 …
对于想要修改 Hosts 文件的朋友来说我们除了自己手动定位到 Hosts 文件使用编辑器编辑外我们还可以通过批处理命令进行编辑。今天大眼仔旭要给大家介绍的是通过使用 Inno Setup 安装包在安装程序的时候对 Hosts 进行修改以达到屏蔽某些应用程序访问某些服务器进行验证的效果。
Windows 主机文件存储 IP 地址和域名有助于将计算机定向到 Internet 或本地网络上的站点。 浏览 Web 通常不需要编辑 Hosts 文件,但是它是阻止有害站点的基本方法,还是一种将 Web 地址与正在开发中的网站绑定在一起的工具。 Hosts 文件设置不正确会导致网站停止加载,因此,如果您无法在线连接某些站点,请检查文件中的错误条目或清除其修改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 #ifdef Unicode
#define A "W"
#else
#define A "A"
#endif
[code]
const
myMark = '由 gnatix 编写'; // 作为标识
function GetFileAttributes(lpFileName: String): Cardinal;
external 'GetFileAttributes{#A}@kernel32.dll stdcall';
function SetFileAttributes(lpFileName: String; dwFileAttributes: Cardinal): Boolean;
external 'SetFileAttributes{#A}@kernel32.dll stdcall';
function LineInFile(sLine, fPath: string): Boolean;
var
aos: TArrayOfString;
n: Integer;
begin
Result:= false;
if LoadStringsFromFile(fPath, aos) then
for n:= 0 to GetArrayLength(aos)-1 do
if aos[n] = sLine then
begin
Result := true;
Exit;
end;
end;
procedure AddHosts(newItem, comments: string);
var
OldFileAttribute: Cardinal;
hfPath, newLine: string;
begin
hfPath := ExpandConstant('{sys}\drivers\etc\hosts');
if not LineInFile(newItem, hfPath) then // 仅添加 Hosts 中还没有的项目
begin
OldFileAttribute:= GetFileAttributes(hfPath);
SetFileAttributes(hfPath, FILE_ATTRIBUTE_NORMAL);
newLine := newItem + ' # ' + myMark;
If comments > ' ' then
newLine := newLine + ' / ' + comments;
SaveStringToFile(hfPath, #13#10 + newLine, True);
SetFileAttributes(hfPath, OldFileAttribute);
end;
end;
procedure RemoveHosts(sItem: string);
var
OldFileAttribute: Cardinal;
hfPath, newLine: string;
stl: TStringList;
n: Integer;
begin
hfPath := ExpandConstant('{sys}\drivers\etc\hosts');
newLine := sItem + ' # ' + myMark;
stl:= TStringList.Create;
stl.LoadFromFile(hfPath);
for n:= stl.Count-1 downto 0 do
if Pos(newLine, stl.Strings[n]) = 1 then
stl.Delete(n);
OldFileAttribute:= GetFileAttributes(hfPath);
SetFileAttributes(hfPath, FILE_ATTRIBUTE_NORMAL);
stl.SaveToFile(hfPath);
stl.Free;
SetFileAttributes(hfPath, OldFileAttribute);
end;
procedure Initializewizard;
begin
AddHosts('0.0.0.0 www.xxx.com', '这是注释'); // 在 Hosts 中添加新项目,带注释
AddHosts('0.0.0.0 www.111.com', ''); // 在 Hosts 中添加新项目,不带注释
end;
procedure DeinitializeUninstall;
begin
RemoveHosts('0.0.0.0 www.xxx.com'); // 从 Hosts 中删除项目
RemoveHosts('0.0.0.0 www.111.com'); // 从 Hosts 中删除项目
end;
把以上代码复制到 Inno Setup 脚本中保存或者根据您的需要再进行修改调整即可。
Windows hosts 文件的功能类似于 DNS 服务器的本地副本,因此如果您要进行自定义域重定向,阻止网站或删除由恶意软件设置的恶意条目,则知道如何进行编辑可能会派上用场。 也就是说,在某些 Windows 版本中对此文件进行更改时,您可能会遇到权限错误和其他问题。
声明:大眼仔旭 | 本文采用署名-非商业性使用-相同方式共享 4.0 国际许可协议[CC BY-NC-SA]进行授权
文章名称:《Inno Setup 如何修改 Hosts 文件解决方案》
文章固定链接:http://www.dayanzai.me/inno-setup-hosts.html
本站资源仅供个人学习交流,请于下载后 24 小时内删除,不允许用于商业用途,否则法律问题自行承担。
文章名称:《Inno Setup 如何修改 Hosts 文件解决方案》
文章固定链接:http://www.dayanzai.me/inno-setup-hosts.html
本站资源仅供个人学习交流,请于下载后 24 小时内删除,不允许用于商业用途,否则法律问题自行承担。
转载声明
猜你喜欢
- 2023-03-31安卓电子书阅读器 Librera PRO 8.8.82 中文多语免费版
- 2019-03-03Flash 相册制作工具 iPixSoft Flash Slideshow Creator 5.1.0.0 中文汉化免费版
- 2023-04-22安卓系统垃圾清理工具 CCleaner for Android 6.9.0 中文免费版
- 2023-03-10哪些低价 Windows 激活密钥真的安全吗?
- 2023-02-13三星 Galaxy S 系列的手机发展历史回顾
相关推荐
全部评论: (0条)
^_^ 暂无评论!
发表评论
最新标签
Autodesk
GitHub
Inno Setup 技巧
Inno Setup 教程
JetBrains
Microsoft
Office
PDF 编辑器
PDF 阅读器
Win10 技巧
Windows 10
Windows 10 技巧
Windows 11 小技巧
Wise Soft
代码编辑器
免费商用字体
免费字体
免费软件
办公软件
固态硬盘检测工具
图像处理工具
图像浏览器
安卓软件
安装包制作工具
屏幕录像
屏幕录像工具
屏幕录像软件
开源字体
开源软件
思维导图
截图工具
数据恢复工具
文本编辑器
格式转换工具
桌面录像
系统优化工具
系统增强
系统增强工具
编程开发
视频播放器
视频转换器
视频转换工具
解压缩工具
音乐播放器
音频转换工具