在Windows作業系統中,控制台的檔案一般是以“.cpl”為尾碼的,下表列出Windows控制台常用的選項及其檔案名稱:
-------------------------------------------------------------------------------------------------
選項 檔案名稱
--------------------------------------------------------------------------------------------------
Internet選項: inetcpl.cpl
ODBC資料來源管理: odbccp32.cpl
電話和數據機選項: telephon.cpl
電源選項: powercfg.cpl
協助工具功能選項: access.cpl
地區和語言選項: intl.cpl
日期和時間: timedate.cpl
聲音和音訊裝置: mmsys.cpl
滑鼠: main.cpl
添加或刪除程式: appwiz.cpl
添加硬體: hdwwiz.cpl
網路連接: ncpa.cpl
系統: sysdm.cpl
顯示: desk.cpl
使用者帳戶: nusrmgr.cpl
遊戲控制器: joy.cpl
語音: sapi.cpl
----------------------------------------------------------------------------------------------------
字型: Fonts
----------------------------------------------------------------------------------------------------
這些是常用的控制台中的選項。
操作:
我們在C#中可以用以下方式開啟操作:
using System.Diagnostics;//在調用命名空間時調用。
//在事件處理中我們可以採用如下方式:
try
{
Process.Start("[帶上以上的檔案名稱全稱]");
}
catch(Win32Exception win32ex)
{
MessageBox.Show("出錯原因:"+win32ex.Message,"出錯",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
樣本:
我們以Internet選項為例進行操作:
我們修改一下上面的代碼為:
using System.Diagnostics;
ProcessStartInfo Info=new ProcessStartInfo();
try
{
Info.FileName="inetcpl.cpl";
Process.Start(Info);
}
catch(Win32Exception win32ex)
{
MessageBox.Show("出錯原因:"+win32ex.Message,"出錯”,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
在程式運行以後出現如下效果:
如果我們在程式中不輸入完整的檔案名稱,將會產生錯誤,並出現如下的提示資訊:
附原始碼:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
namespace CsharpCallCPL
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 表單設計器支援所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 72);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "調用";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋體", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(40, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(203, 24);
this.label1.TabIndex = 1;
this.label1.Text = "C#調用控制台範例";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(296, 125);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
ProcessStartInfo Info=new ProcessStartInfo();
try
{
Info.FileName="inetcpl.cpl";
Process.Start(Info);
}
catch(Win32Exception win32ex)
{
MessageBox.Show("出錯原因:"+win32ex.Message,"出錯",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}