System.Diagnostics.Process _pCmd; _pCmd = new System.Diagnostics.Process(); _pCmd.StartInfo.FileName = "cmd.exe"; _pCmd.StartInfo.UseShellExecute = false; _pCmd.StartInfo.RedirectStandardOutput = true; _pCmd.StartInfo.RedirectStandardInput = true; _pCmd.StartInfo.CreateNoWindow = true; _pCmd.Start();
//執行目標指令 _pCmd.StandardInput.WriteLine("shutdown -a"); 再一例:
Regex reg = new Regex(@"[\u4e00-\u9fa5]");//正則表達式 if (reg.IsMatch(textBox1.Text) || reg.IsMatch(textBox2.Text)) { MessageBox.Show("不能含有漢字"); return; } if (textBox2.Text.Length < 8) { MessageBox.Show("密碼8位以上"); return; } _strWrite = String.Format("netsh wlan set hostednetwork mode=allow ssid={0} key={1}", this.textBox1.Text, this.textBox2.Text); _pCmd.StandardInput.WriteLine(_strWrite);
C.通過C# (.NET) 進行系統管理 自動關閉指定進程(如閃訊的“singleNet”進程) 取代了人工在任務管理器中查找進程,解救密集恐懼症患者~
Process[] processes = Process.GetProcessesByName(ProcessName); foreach (Process instance in processes) { try { if (instance.ProcessName == ProcessName) instance.Kill();; } catch { }
調用\Windows\system32\hnetcfg.dll,即能使用“NETCONLib”實現自動共享 (關鍵)
try { string connectionToShare = this.textBox3.Text; // 被共享的網絡連接 string sharedForConnection = this.textBox4.Text; // 需要共享的網絡連接 NetSharingManager manager = new NetSharingManager(); var connections = manager.EnumEveryConnection; foreach (INetConnection c in connections) { var props = manager.NetConnectionProps[c]; var sharingCfg = manager.INetSharingConfigurationForINetConnection[c]; if (props.Name == connectionToShare) { sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC); } else if (props.Name == sharedForConnection) { sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE); } } } catch { MessageBox.Show("請打開網絡和共享中心·查看是不是已經連接Internet!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
取代手動進入“網絡共享中心->更改適配器設置->singleNetPPPoE屬性->共享->勾選允許分享internet->選擇分享目標”這一連串操作,懶人福音~