开发者

Windows Network Information Manipulation

开发者 https://www.devze.com 2023-03-16 15:14 出处:网络
I am working as a trainee engineer in a networking firm and am getting annoyed by h开发者_运维知识库aving to change the IP information from time to time.

I am working as a trainee engineer in a networking firm and am getting annoyed by h开发者_运维知识库aving to change the IP information from time to time.

I am in need of building a software to help me change these details easily. I have managed to set the IP information. But I still have problems.

  1. I need to run the program as Administrator [right click], is there a way to program to prompt for it at startup?
  2. How can I change adapter to DHCP?

The code is quite long, and I hope not to fill bore you with it. But I have been using Management

  • Management Class
  • Management Base Object
  • Management Object Collection in my development.

I'd prefer to make my own program to develop my programming skills. But if there is an application to do it, I don't mind knowing.


I hope this answer gives you some insisght and direction to go.

Okay, the network adapter one isn't that straight forward, but I believe you can achieve it with WMI, specially this WMI object here. The MSDN documentation tells you all the properties, methods (which there are for setting DHCP etc) and the datatypes and values it takes. This may be one approach as using WMI through C# is pretty easy. I wish I could provide you an example, but I've never used that specific WMI class before. You can also access the above WMI class through the Visual Studio Server Explorer, which you can see here. ..and it has your "EnableDHCP" method you are probably looking for.

Windows Network Information Manipulation

As far as asking for your program to run with administrative priviledges, here is the code from my setup project in my framework. What this does is before it runs any sort of form or logic, requests the "runas" verb which invokes UAC (if Windows has its Vista/7, and requests admin priviledges from the user)

namespace Setup {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using Setup.Forms;
    using System.Security.Principal;
    using System.Diagnostics;

    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            bool administrativeMode = principal.IsInRole(WindowsBuiltInRole.Administrator);

            if (!administrativeMode) {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.Verb = "runas";
                startInfo.FileName = Application.ExecutablePath;
                try {
                    Process.Start(startInfo);
                }
                catch {
                    return;
                }
                return;
            }

            Application.Run(new ShellForm());
        }
    }
}

As far as a program to do it, Windows Network Connection Manager? I know its cumbersome because of all the dialogs, but.. its already there.


I once had to write a very similar program. I used some of the source code from these two projects to help me get started: Chameleon Project and Configuring TCP/IP Settings using WMI and C#

The Chameleon Project is a C# project to help change network settings of a particular adapter. The other project is a tutorial on how to use C# to change network settings using WMI and C#. You can look at the source code and learn from it to help you make your own software that does what you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消