C++/c#修改hosts文件

 

c++

#include<iostream>
#include<fstream>
#include <WTypes.h>
#include <Winbase.h>
using namespace std;
int main()
{
ofstream outf;
char * lpFileName="C:\\Windows\\System32\\drivers\\etc\\hosts";
DWORD   dwAttribute   =   ::GetFileAttributes(lpFileName);
if(dwAttribute   &   FILE_ATTRIBUTE_READONLY)
{
dwAttribute   &=   ~FILE_ATTRIBUTE_READONLY;
SetFileAttributes(lpFileName,dwAttribute);
}
outf.open("C:\\Windows\\System32\\drivers\\etc\\hosts" , ifstream::in);//要覆盖文件,把最后一个参数去掉
outf<<"\n# Copyright (c) 1993-1999 Microsoft Corp.";
outf<<"\n#";
outf<<"\n# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.";
outf<<"\n#";
outf<<"\n# This file contains the mappings of IP addresses to host names. Each";
outf<<"\n# entry should be kept on an individual line. The IP address should";
outf<<"\n# be placed in the first column followed by the corresponding host name.";
outf<<"\n# The IP address and the host name should be separated by at least one";
outf<<"\n# space.";
outf<<"\n#";
outf<<"\n# Additionally, comments (such as these) may be inserted on individual";
outf<<"\n# lines or following the machine name denoted by a '#' symbol.";
outf<<"\n#";
outf<<"\n# For example:";
outf<<"\n#";
outf<<"\n# 102.54.94.97 rhino.acme.com # source server";
outf<<"\n# 38.25.63.10 x.acme.com # x client host";
outf<<"\n127.0.0.1 localhost";
outf<<"\n";
outf.close();
return 0;
}

////追加

#include<iostream>
#include<fstream>
#include <STRING>
#include <WTypes.h>
#include <Winbase.h>
using namespace std;
int main()
    {
    ofstream outf;
    ifstream inf;
    cout<<" 开始设置本地域名,如果杀毒软件弹出窗口,请选择允许/信任\n";
    char * lpFileName="C:\\Windows\\System32\\drivers\\etc\\hosts";
    DWORD   dwAttribute   =   ::GetFileAttributes(lpFileName);
    DWORD   wrAttribute=dwAttribute;
    if(dwAttribute   &   FILE_ATTRIBUTE_READONLY)
    {
        wrAttribute   =   dwAttribute&~FILE_ATTRIBUTE_READONLY;
        SetFileAttributes(lpFileName,wrAttribute);//去掉只读属性
    }
    inf.open(lpFileName,ios::in);
    string line;
    bool haskinpan=false;
    system("color 0A");
    while(getline(inf,line))
    {
        //cout<<line<<endl;
        if(line=="192.168.0.100 local.kinpan.com")
            {
            haskinpan=true;
            cout<<" 已经设置,您可以通过 local.kinpan.com 访问本地测试网站 \n";
            break;
        }
    }
inf.close();
if(haskinpan==false){
    outf.open("C:\\Windows\\System32\\drivers\\etc\\hosts" , ios::app);//追加
    outf<<"\n192.168.0.100 local.kinpan.com";
    outf<<"\n";
    outf.close();
    cout<<"设置成功! 您可以通过 local.kinpan.com 访问本地测试网站\n";
}
    SetFileAttributes(lpFileName,dwAttribute);//恢复只读属性
    cout<<" 按任意键关闭\n";
    system("pause");
    return 0;
}

 c#

//using System.IO;
string fpath = @"C:\WINDOWS\system32\drivers\etc\hosts";
File.SetAttributes(@fpath, FileAttributes.Normal);
FileStream fs = new FileStream(@fpath, FileMode.Append); //写入的方式可改变,这里是追加
StreamWriter sw = new StreamWriter(fs);

//写入的一行内容
sw.WriteLine("127.0.0.1     www.tt.com");

//写入大量内容前面加 “@”符号
sw.WriteLine(@"0.0.0.0 www.bfaft.com
0.0.0.0 bbs.bfaft.com
0.0.0.0 bfaft.com
");
sw.Close();
fs.Close();

 

 

上一篇:开发一个简单的工具,导出github仓库所有issue列表


下一篇:Java基本开发环境搭建