文章總算進入程式部分

先前說過對於批次檔蠻有興趣的

想說一些小小的指令讓自己方便下手 點擊按鈕就可以執行 所以就統整在同一個圖形介面就不用找來找去(懶惰XDD

直接切入正題

先拉好必要的元件 就是一些BUTTON 如圖( 寫C#就從BUTTON開始!!

image

接著快點兩下其中一個BUTTON進去撰寫程式(或右鍵選擇檢視程式碼

 

如標題所示會用到Process來執行CMD指令所以程式上方要加:

using System.Diagnostics;

 

再來要使用Process,來宣告

Process p = new Process();

 

接著就是程式核心

p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;// 接受來自呼叫程式的輸入資訊
p.StartInfo.RedirectStandardOutput = true;// 由呼叫程式獲取輸出資訊
p.StartInfo.RedirectStandardError = true;//重定向標準錯誤輸出
p.StartInfo.CreateNoWindow = true; //不跳出cmd視窗
p.Start();// 啟動程式
p.StandardInput.WriteLine("這邊打上你要輸入的指令");//向cmd視窗傳送輸入資訊
p.Close();//關閉程式

其中

Starinfo屬性,可取得或設定要傳給process的start()的屬性

例如:StartInfo.FileName = "cmd.exe"

 

如果指令需要工作管理員身分記得參考之前文章:

https://docu2019ment.pixnet.net/blog/post/4665179

 

以下就是我維修電腦比較常用到的指令

又或者是我懶得從很裡面的地方去找

可以加入各位常用的指令

 

完整程式碼如下:--------------------------------------------------------------------

using System;
using System.Windows.Forms;
using System.Diagnostics;

namespace 常用電腦維修快捷鍵._0//改成專案名稱
{
    public partial class Form1 : Form
    {
        Process p = new Process();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true; //不跳出cmd視窗
            p.Start();
            p.StandardInput.WriteLine("taskmgr");//呼叫工作管理員
            p.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true; //不跳出cmd視窗
            p.Start();
            p.StandardInput.WriteLine("inetcpl.cpl ,0");//網際網路選項
            p.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true; //不跳出cmd視窗
            p.Start();
            p.StandardInput.WriteLine("control printers");//裝置和印表機
            p.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true; //不跳出cmd視窗
            p.Start();
            p.StandardInput.WriteLine("devmgmt.msc");//裝置管理員
            p.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true; //不跳出cmd視窗
            p.Start();
            p.StandardInput.WriteLine("regedit.exe");//登入檔
            p.Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true; //不跳出cmd視窗
            p.Start();
            p.StandardInput.WriteLine("diskmgmt.msc");//磁碟管理
            p.Close();
        }
    }
}

程式碼結束-----------------------------------------------------------------------------

接著來測試一下,成功

image

懶惰萬歲XDD

參考資料

https://www.itread01.com/content/1548903965.html

https://docs.microsoft.com/zh-tw/dotnet/api/system.diagnostics.process.startinfo?view=netcore-3.1

 

 

 

arrow
arrow

    docu2019ment 發表在 痞客邦 留言(0) 人氣()