<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      C#調用控制面板選項

      [摘要]C#是一門由Microsoft新推出的開發語言,它是基于Microsoft的.NET Framework平臺基礎上的新興的開發工具。正因為它是由Microsoft公司推出的,所以它對Microsof...
      C#是一門由Microsoft新推出的開發語言,它是基于Microsoft的.NET Framework平臺基礎上的新興的開發工具。

      正因為它是由Microsoft公司推出的,所以它對Microsoft的所有產品的兼容性與相互操作性是其它公司開發出的編程語言所不及的。Microsoft開發的Windows操作系統與C#之間的關系也非常緊密。從而實現了C#對Windows的無縫操作。

      下面,我們就以“C#對Windows控制面板中的選項進行操作”為題講述一下它們之間的聯系。

      在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);

                    }

               }

           }

      }



      日韩精品一区二区三区高清