[Developer Note] use. Net to implement your IP Switch

Source: Internet
Author: User

Released on: 2007.4.17 by anytao

2007 indicate the source of anytao.com.

Download: [anytao. iphelper] [Code download, recent upload]

This article will introduce the following content: • batch processing file applications • calling external applications • File Processing

1. Introduction

I often run around in the company, customer, and home. Switching IP addresses is a daily task. So I am eager to develop a small application to implement fast IP address switching. The execution of the prepare command, and then use the. NET Framework to call the BAT file. So far, you can act immediately.

2. Basic Principles

According to the above solution, the solution of my small system named anytao. iphelper is as follows:

  • Use the batch file upload netsh.exe command to load IP addresses;
  • Use the. NET process method to call external applications;
  • With the progress control and file retrieval functions.

Therefore, it is necessary to simply sort out the operating principles of the system and reveal the essence.

2.1. netsh tool Introduction

I will not introduce the netsh command for a long time here. Baidu can find a more detailed netsh command manual, which will take you a day to study. Since this article focuses on applications, I will explain from the actual tactics. The detailed Martial Arts secret is to be tailored to the audience. Here I will only provide a reference: netsh command details, netsh command usage.

In this article, refer to the following steps to implement your netsh command processor:

  1. Open notepad and enter the following information:

    @ Echo anytao is switching your IP now.
    @ Echo off
    Netsh interface IP Set address local connection static 172.1.1.226 255.0.0.0 172.30.7.254 1
    Netsh interface IP Set DNS local connection static 200.121.5.1

    Echo www.anytao.com
    Echo congratulations, done.
    Pause

    Note: enter your IP address, subnet mask, default gateway address, and DNS server address in the preceding format. In the English version of the operating system, change "Local Connection" to "Local Area Connection ".

  2. Save the text as a batch file and save it as: Home. bat.

  3. Click the batch file to update your IP address to the above value.

2.2. Introduction to the Process Method

The process method, which is located in the system. Diagnostics namespace. Msdn is defined:ProcessThe component provides access to processes running on the computer. To start, stop, control, and monitor applications,ProcessComponents are very useful tools. UseProcessYou can obtain the list of running processes or start a new process.ProcessThe component is used to access system processes. InitializationProcessYou can use this component to obtain information about the currently running process. This information includes the thread set, loaded modules (. dll and. EXE files), and performance information (such as the amount of memory currently used by the process ). Therefore, opening external batch processing files using the process method is a recommended method in a hosted environment.

Of course, opening external programs using the process method is not the only way. The simplest thing is to call the winapi program for implementation. Of course, this involves the scope of unmanaged code, we are no longer arrogant.

In my other article "Open URL in winform application", I have also discussed it. For specific implementation, see the analysis in the Code section.

2.3. The basic system structure is as follows:

3. Implementation Method:

3.1 create a Windows form application, set the program name to anytao. iphelper, and drag a ComboBox and a button control to the form file, named cbbip and btnipconfig respectively, as shown in:

3.2 double-click btnipconfig to implement its click event, as shown in the following code:

Private void btnipconfig_click (Object sender, eventargs E)
{
Try
{
// Use the process method to load external applications
Process myprocess = new process ();
Myprocess. startinfo. filename = application. startuppath + @ "\ config \" + cbbip. selecteditem. tostring () + ". Bat ";
MessageBox. Show (myprocess. startinfo. filename );
Myprocess. startinfo. useshellexecute = false;
Myprocess. startinfo. redirectstandardoutput = true;
Myprocess. Start ();
Myprocess. waitforexit ();
}
Catch (filenotfoundexception ex)
{
MessageBox. Show (ex. Message );
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
}

3.3 of course, we also need to perform operations to obtain the file directory during program loading, as shown below:

Private void frmmain_load (Object sender, eventargs E)
{
// Load the configuration file to the drop-down list
String Path = application. startuppath + @ "\ config \";
If (directory. exists (PATH ))
{
Directoryinfo dirinfo = new directoryinfo (PATH );
Foreach (fileinfo fi in dirinfo. getfiles ())
{
Cbbip. Items. Add (path. getfilenamewithoutextension (Fi. fullname ));
}
}
}

3.4 of course, you can add the configured netsh.exe batch processing file to the target location and then implement your own iPhone per.

3.5 congratulations, the simple iphelper has been implemented. Run your program and enjoy the convenience brought by quick IP configuration.

 

4. System Outlook

Anytao. iPhone per only ignited a rapid application. I also enjoyed this kind of convenience in my daily work. I also needed iPhone per in my Emma machine, it is like the love and care I bring to her every day. Hey, you can also use this method to write code. It makes our program life more harmonious.

To put it bluntly, iPhone per is just a tool for proxy, and more ideas and designs can be integrated into it for enrichment. I will gradually improve the following aspects in the future:

  • Design IP and ipconfig classes to improve the system architecture in an object-oriented manner;
  • The IP entry function is added to dynamically add IP options without manual implementation;
  • More desktop network management platforms are integrated.

5. Download after conclusion

The main purpose of this article is to take an example as the starting point to explain how to load external applications in a hosted environment using the process method, using process can easily open websites, emails, external programs, scripts, and so on in your applications, so it is worth learning.
Because the development-based article is the first attempt. I don't know if you want to laugh at it. The small tools hope to bring convenience to your life.
The Code part must be partially improved. We will provide the program part first today. I will upload the code part as soon as possible. Please forgive me. I hope this little tool can help you.

Download: [anytao. iphelper] [Code download, recent upload]

 

2007 indicate the source of anytao.com.

This post uses"Status quo"No warranties are provided and no rights are granted.
This posting is provided "as is" with no warranties, and confers no rights.

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.