Write IP address converters with Delphi

Source: Internet
Author: User
Tags error handling trim

Now the school and the company's friends have mostly encountered this situation: the computer needs to be often in the internet and between the LAN conversion, each conversion in addition to changing the network cable, but also need to modify the configuration of networks (IP address, gateway, DNS, etc.), quite troublesome. How can one step in a window? Some companies offer software such as Ipchanger, Easyproxy, and so on, but unfortunately these software are not free software and can only be tried for a period of time.

Therefore, the author after trying to debug a very simple, easy way for everyone to refer to use.

First look at the principle:

The netsh command for Windows modifies the network configuration by creating and invoking a text file that records network configuration. Mainly includes two commands:

Netsh interface dump > relative path \ netcfg1.txt

This command saves the existing network configuration of your computer in the Netcfg1.txt file.

Netsh-f Netcfg1.txt

This command allows you to apply the network configuration saved in the Netcfg1.txt file to your computer.

Based on the above two commands, the author uses Delphi6.0 to compile the small program, and further improve it. The results of the operation are shown in the following illustration:

As shown in the figure above, the main functions of small software are divided into two parts: network settings and network substitution. Because the vast majority of computers in two networks are interchangeable, so this program only achieved two network interchange, more network interchange principle is the same.

Network Settings "Internet" and "LAN" two button click event is the left Network Settings area IP address, gateway, DNS and other information recorded in the text. One of the two major difficulties encountered is the IP address when the error occurred in the processing and save the text file.

The code is as follows:

Procedure Tform1.bitbtn2click (Sender:tobject); Click events for the LAN button
Begin
FileAppendProc1 ();
Radiobutton2.checked:=true;
End
Procedure Tform1.fileappendproc1 (); function to save a file
var netfile:textfile;
netfilename:string;
fileappend,tempstr:string;
Begin
Netfilename: = ' netcfg2.txt ';
Fileappend: =S5+MASKEDIT1.TEXT+S1+MASKEDIT2.TEXT+S2+MASKEDIT3.TEXT+S3+S4;
Messagedlg (' This is commend ' +fileappend,mtinformation,[mbok],0);
AssignFile (netfile, ' net.txt '); Net.txt files to be placed in the same directory in the application
Reset (Netfile);
Begin
Try
Memo1.lines.text:= ';
While does Eof (Netfile) do
Begin
READLN (NETFILE,TEMPSTR);
MEMO1.LINES.ADD (TEMPSTR);
End
Finally
Begin
CloseFile (Netfile);
End
End
AssignFile (Netfile,netfilename);
Rewrite (Netfile);
Try
Writeln (Netfile, "");
Finally
CloseFile (Netfile);
End
MEMO1.LINES.ADD (Fileappend);
Memo1.Lines.SaveToFile (Netfilename);
End
End
Procedure Tform1.maskedit1exit (Sender:tobject); IP Address input error handling
var Ip1,ip2,ip3,ip4:integer;
Begin
if (copy (maskedit1.text,1,3) = ') or (copy (maskedit1.text,5,3) = ') or (copy (maskedit1.text,9,3) = ') or (copy (MaskEdit1 . text,13,3) = ') Then
Begin
ShowMessage (' Please note, cannot have airspace value ');
Maskedit1.setfocus;
End
if (copy (maskedit1.text,1,3) <> ') and (copy (maskedit1.text,5,3) <> ') and (copy (maskedit1.text,9,3) <& gt; ' ') and (copy (maskedit1.text,13,3) <> ') then
Begin
Ip1:=strtoint (Trim (copy (maskedit1.text,1,3));
Ip2:=strtoint (Trim (copy (maskedit1.text,5,3));
Ip3:=strtoint (Trim (copy (maskedit1.text,9,3));
Ip4:=strtoint (Trim (copy (maskedit1.text,13,3));
if (ip1<0) or (ip1>254) or (ip2<0) or (ip2>254) or (ip3<0) or ip3>254) the N
Begin
ShowMessage (' Your input is not correct, please re-enter! ');
Maskedit1.setfocus;
End
End
End

The right side of the network replacement function is relatively simple, through two RadioButton controls to choose which network to use, and then click the "OK" button.

procedure TForm1.Button1Click(Sender: TObject); //"确定"按钮的单击事件
begin
   if RadioButton1.Checked then winexec('netsh -f netcfg1.txt',sw_normal) ;
   if RadioButton2.Checked then winexec('netsh -f netcfg2.txt',sw_normal) ;
end;

In this way, the function is realized, this small software is simple and easy to use, in my classmates around has been widely circulated, I hope we can learn from the Simple network configuration method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.