因为C#是由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);
}
}
}
}
分享到:
相关推荐
第1章 认识C#及开发环境 1.1 C#概述 2 1.1.1 C#发展历程 2 1.1.2 C#语言编程环境 2 1.2 .NET Framework 2.0简介 2 1.2.1 什么是.NET Framework 2.0 2 1.2.2 .NET Framework 2.0特性 3 1.3 安装集成...
明日科技出版的c#开发典型模块大全配套光盘,要的可以下载,因大小受限,分开上传! 第1章 认识C#及开发环境 1.1 C#概述 1.1.1 C#发展历程 1.1.2 C#语言编程环境 1.2.NETFramework2.0简介 1.2.1 什么是....
第4页 C#(WINFORM)学习 } Enum 需要放在 class 外面,才能被其它 class 的程序调用。 C#关键字 关键字 abstract as base bool break byte case catch char checked class const continue decimal default ...
第1章 认识C#及开发环境 1.1 C#概述 1.1.1 C#发展历程 1.1.2 C#语言编程环境 1.2.NETFramework2.0简介 1.2.1 什么是.NETFramework2.0 1.2.2.NETFramework2.0特性 1.3 安装集成开发环境VisualStudio2005 1.3.1 安装...
明日科技出版的c#开发典型模块大全配套光盘,要的可以下载,因大小受限,分开上传! 第1章 认识C#及开发环境 1.1 C#概述 1.1.1 C#发展历程 1.1.2 C#语言编程环境 1.2.NETFramework2.0简介 1.2.1 什么是....
16 <br>0033 Return语句的使用 17 <br>0034 如何实现无限循环 17 <br>0035 巧用foreach语句控制控件 18 <br>0036 有效使用switch case语句 18 <br>2.3 运算符 19 <br>0037 如何使用...
实例196 隐藏控制面板中的声音设备 281 实例197 获取显示设备的当前显示模式 282 实例198 获取声音设备的名称及PNPDeviceID 283 实例199 语音计算器 284 5.5 多媒体应用 286 实例200 开机祝福程序 286 实例201...
实例196 隐藏控制面板中的声音设备 281 实例197 获取显示设备的当前显示模式 282 实例198 获取声音设备的名称及PNPDeviceID 283 实例199 语音计算器 284 5.5 多媒体应用 286 实例200 开机祝福程序 286 实例201...
实例196 隐藏控制面板中的声音设备 281 实例197 获取显示设备的当前显示模式 282 实例198 获取声音设备的名称及PNPDeviceID 283 实例199 语音计算器 284 5.5 多媒体应用 286 实例200 开机祝福程序 286 实例201...
在C#中DateTime是一个包含日期、时间的类型,此类型通过ToString()转换为字符串时,可根据传入给Tostring()的参数转换为...在Windows系统中的区域和语言(位置:控制面板 → 区域和语言)选项中可设置日期和时间的格式。
实例196 隐藏控制面板中的声音设备 281 实例197 获取显示设备的当前显示模式 282 实例198 获取声音设备的名称及PNPDeviceID 283 实例199 语音计算器 284 5.5 多媒体应用 286 实例200 开机祝福程序 286 实例201...