首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用c#更改windows中的DNS

使用c#更改windows中的DNS
EN

Stack Overflow用户
提问于 2018-06-09 05:31:44
回答 1查看 806关注 0票数 0

很抱歉弄得一团糟,我几天前就开始了,我还在努力学习。我的代码:

代码语言:javascript
复制
private void button1_Click(object sender, EventArgs e)
        {
            ManagementScope oMs = new ManagementScope();
            ObjectQuery oQuery =
                new ObjectQuery("Select * From Win32_NetworkAdapter");
            ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
            ManagementObjectCollection oReturnCollection = oSearcher.Get();
            foreach (ManagementObject oReturn in oReturnCollection)
            {
                if (oReturn.Properties["NetConnectionID"].Value != null)
                {
                    // I want the result from this pasted into the "Network Adapter" prompt           
                    Console.WriteLine(oReturn.Properties["NetConnectionID"].Value);
                    // This is probably 100% wrong, sorry.
                    String NetworkAdapter = Console.WriteLine(oReturn.Properties["NetConnectionID"].Value);

                    Process process = new Process();
                    process.StartInfo.FileName = "cmd.exe";
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.UseShellExecute = false;
                    process.Start();
                    // I want to insert the Name of a network adapter into this command prompt, but I can't seem to manage it.
                    process.StandardInput.WriteLine("netsh interface ipv4 set dns "NetworkAdapter" static 8.8.8.8");
                    process.StandardInput.WriteLine("netsh interface ipv4 add dns "NetworkAdapter" 8.8.4.4 index=2");
                    process.StandardInput.Flush();
                    process.StandardInput.Close();
                    process.WaitForExit();
                    Console.WriteLine(process.StandardOutput.ReadToEnd());
                    Console.Read();
                }
            }
       }

这个想法是,当我按下按钮时,它将检测到我的活动网络适配器名称,并通过cmd.exe提示将其粘贴到标准的dns更改中。我开始觉得我让自己变得更难了。

谢谢你的帮助。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50768640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档