windows.open 参数

后端 (32) 2023-10-31 11:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说windows.open 参数,希望能够帮助你!!!。

前一篇玩了一下组播收发,今天再来分享一下在Windows和Linux下修改网卡的网络参数;

1
、在Windows下通过Management来实现:


windows.open 参数_https://bianchenghao6.com/blog_后端_第1张
2、Ubuntu下实现就相对麻烦一点:

①先通过 route -n 获取到使用的网络接口
/// <summary>/// 1、先通过 route -n 获取到使用的网络接口/// </summary>private string GetCurrentUseInterface(){ string interfaceName = ; var process = new Process() { StartInfo = new ProcessStartInfo { FileName = "/bin/bash", Arguments = "-c \"route -n\"", RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, } }; process.Start(); //这一行没用是他:Kernel IP routing table process.StandardOutput.ReadLine(); ////这一行没用是他:Destination Gateway Genmask Flags Metric Ref Use Iface process.StandardOutput.ReadLine(); while(!process.StandardOutput.EndOfStream) { var line = process.StandardOutput.ReadLine(); if(line.StartsWith("0.0.0.0")) { var match = Regex.Match(line, @"\w+$"); if(match.Success) { interfaceName = match.Value; break; } } else if(interfaceName == ) { var match = Regex.Match(line, @"\w+$"); if(match.Success) { interfaceName = match.Value; } } } if(interfaceName != ) { Console.WriteLine($ "正在使用的网络接口是:{interfaceName}"); } else { Console.WriteLine("未找到正在使用的网络接口。"); } return interfaceName;}

②、创建网络参数配置:
// 创建网络配置config = $@ "network:version: 2renderer: networkdethernets:{ interfaceName}: dhcp4: noaddresses: [ { ipAddress } /{subnetMask}] routes: -to: default via: { gateway } nameservers: addresses: [ { dns }] ";}
③、将网络配置写入临时文件
string tempConfigPath = Path.Combine(Directory.GetCurrentDirectory(), "tempConfigPath.yaml");// 将网络配置写入临时文件File.WriteAllText(tempConfigPath, config);

④、通过sudo mv命令执行最后一步操作
// 使用sudo mv命令将临时文件移动到/etc/netplan/01-netcfg.yamlstring command = $ "sudo mv {tempConfigPath} /etc/netplan/01-netcfg.yaml && sudo netplan apply";var processInfo = new ProcessStartInfo("bash", "-c \"" + command + "\""){ RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true};var process = Process.Start(processInfo);process.WaitForExit();
3、上面操作执行时,会让输入管理员密码,毕竟咱是服务来处理;所以我们提前设置一下,让所有的dotnet运行的时都不需要输入密码windows.open 参数_https://bianchenghao6.com/blog_后端_第2张;
①、终端输入:(pi是当前机器的用户名;/usr/bin/dotnet是安装路径)
echo 'pi ALL=(ALL) NOPASSWD: /usr/bin/dotnet ' | sudo tee -a /etc/sudoers

②、新建一个文件"RunApp.sh"
#!/bin/bashsudo dotnet UpdateIPDemo.dll

③、在终端运行

bash RunApp.sh

最终简单效果就这么完事了;
以后有时间的话,可以再去摸索一下更复杂的效果
windows.open 参数_https://bianchenghao6.com/blog_后端_第3张
;编程不息、Bug不止、无Bug、无生活
;

bug
的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;
今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!




今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。