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