C#基础知识学习笔记3.docx
- 文档编号:8096634
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:19
- 大小:22.88KB
C#基础知识学习笔记3.docx
《C#基础知识学习笔记3.docx》由会员分享,可在线阅读,更多相关《C#基础知识学习笔记3.docx(19页珍藏版)》请在冰豆网上搜索。
C#基础知识学习笔记3
33;;Server对象:
publicpartialclass_Default:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
//Server对象:
提供了访问远程服务器信息的方法和属性
stringstr=Server.MachineName;//获取主机的名称
stringstr1=Server.MapPath("Default.aspx");//获取该文件所在的物理地址
Response.Write(str+"
");
Response.Write(str1+"
");
Response.Write(Server.HtmlDecode("21天学通C#")+"
");//HTML代码被执行
Response.Write(Server.HtmlEncode("21天学通C#")+"
");//HTML代码不被执行
Response.Write(Request.ServerVariables["SERVER_NAME"]);//输出域名
Response.Write("
");
Response.Write(Server.ScriptTimeout.ToString());//输出脚本超时时间
}
}
34;;Cookie对象:
publicpartialclassC67Cookie:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
//Cookie对象:
就是保存用户端硬盘上的一个文本文件,可以存储有关的会话或应用程序的信息,和Session对象有些类似,不同在于:
Cookie的信息是保存在服务端上的,而Cookie则是保存在客户端上的。
其特点:
//数据保存在客户端上的;
//只能保存字符串类型的数据;其他类型则需要转化为字符串类型,Session则可以保存任何类型的数据
//默认周期,可以手动设置,最大50年
//Cookie有两种类型:
临时性和持久性(这就是如登陆信息保留多天)
//可通过Cookie的名称不同来区分不同的Cookie,Cookie对象的设置如下:
在服务器的Response对象的Cookie集合中增加一个Cookie。
Response对象就会把Cookie集合中的所有Cookie信息都发送到客户端中。
//如果客户端就能用Cookie设置为Cookie禁用,则Sessoin也无法使用。
//Cookie的操作:
//
//1)添加Cookie
HttpCookiecookie=newHttpCookie("Info");//定义为Cookie对象以及名为Info的项
DateTimedt=DateTime.Now;
TimeSpants=newTimeSpan(1,0,0,0);//Cookie有效作用时间按
cookie.Expires=dt.Add(ts);//添加过期时间
//为Cookie添加属性
cookie.Values.Add("user","cxbkkk");//注意这里为values
cookie.Values.Add("userID","1203");//前面一项为字段,后面一项为其参数
Response.AppendCookie(cookie);//确定写入Cookie中
//
//读取Cookie操作
if(Request.Cookies["Info"]!
=null)
{
stringstr=Convert.ToString(Request.Cookies["Info"].Values["user"]+Convert.ToString(Request.Cookies["Info"].Values["userID"]));
if(str=="")
{
Response.Write("Cookie为空");
}
else
{
Response.Write(str);
}
}
else
{
Response.Write("有错误!
");
}
//
//修改Cookie
Response.Cookies["Info"]["user"]="2";//如有表明Info为Cookie的名称,而user为其字段
Response.Cookies["Info"].Expires=DateTime.Now.AddDays
(1);
//
//删除Cookie下的某个属性
HttpCookiemyCookie=Request.Cookies["Info"];
myCookie.Values.Remove("userID");//删除该属性
myCookie.Expires=DateTime.Now.AddDays
(2);//设置Cookie的过期时间
Response.Cookies.Add(myCookie);
//删除所有的Cookie
intlimit=Request.Cookies.Count-1;
for(inti=0;i { myCookie=Request.Cookies[i]; myCookie.Expires=DateTime.Now.AddDays(-1); Response.Cookies.Add(myCookie); } } } 35;;Cache对象简介: //Cache对象: 其实就是一种缓冲技术,通过浏览访问远程站点时,会有一些信息被保存在本地计算机中,这些信息其实就是保存在Cache中,使用C爱车对象保存的信息一般是可以公开的,不可用于保存密码。 //操作1): 添加缓存 Cache["zhou"]="一个Cache信息";//zhou是变量,而"一个Cache信息"是变量容;这行可不要,因为下一行其实包含这行的容 Cache.Insert("zhou","一个Cache信息"); stringstrCache=Cache["zhou"].ToString(); Response.Write(strCache+" Cache.Remove("zhou");//删除 36;; publicpartialclassC69_综合练习: System.Web.UI.Page { protectedvoidPage_Load(objectsender,EventArgse) { Application["app1"]="zhouapp1"; Application["app2"]="zhouapp2"; Application["app3"]="zhouapp3"; Application["app4"]="zhouapp4"; string[]App=newstring[Application.Count]; App=Application.AllKeys;//返回全部Application对象变量名到一个字符串数组中 for(inti=0;i { Response.Write(App[i].ToString()+"");//输出为Application的对象(变量名) } //结果为: //app1 //app2 //app3 //app4 } } 37;;//trycatchfinally中可以有多个catch但是当前面的catch完成之时,则 后面的catch则无效。 例如: namespaceConsoleApplication2 { classProgram { staticvoidMain(string[]args) { try { stringstr=null; if(str==null) { thrownewArgumentException();//在向方法提供的其中 一个参数无效时引发的异常。 } } catch(ArgumentExceptione) { Console.WriteLine("{0}第一个异常",e.Message); } catch(Exceptione) { Console.WriteLine("{1}第二个异常",e.Message); } Console.Read(); } } } 38;; namespaceWindowsFormsApplication1 { //作用将菜单栏上的项目提示,输出到状态栏上 publicpartialclassForm1: Form { publicForm1() { InitializeComponent(); } //定义一作用函数 privatevoidshowHelpInfo(object_targe) { //获取菜单项的引用,比如: “文件”该对象,抽象 ToolStripMenuItemzhou=(ToolStripMenuItem)_targe; // //将提示信息输出到状态栏中 this.toolStripStatusLabel1.Text=zhou.ToolTipText;//这行 和下一行的功能是一样的 //this.statusStrip1.Items[0].Text= zhou.ToolTipText;//ToolTipText是: 提示信息 } privatevoidForm1_Load(objectsender,EventArgse) { } privatevoid新建ToolStripMenuItem_MouseEnter(objectsender, EventArgse) { this.showHelpInfo(sender); } privatevoid打开ToolStripMenuItem_MouseEnter(objectsender, EventArgse) { this.showHelpInfo(sender); } } } 39;;控件使用: namespaceWindowsFormsApplication1 { publicpartialclassC91: Form { publicC91() { InitializeComponent(); } //dateTimePicker的使用: 时间控件 privatevoiddateTimePicker1_CloseUp(objectsender,EventArgs e)//当日期控件下拉菜单收起时的作用 { //stringstr=dateTimePicker1.Text;//显示年月日 stringstr=dateTimePicker1.Value.ToString();//显示年月日 时分秒 label2.Text=str; } //listbox列表控件的使用 //selectmode选择模式: 单行多行 privatevoidC91_Load(objectsender,EventArgse) { listBox1.Items.Add("篮球"); listBox1.Items.Add("足球"); listBox1.Items.Add("排球"); //TreeView控件: //属性: Nodes里加跟或子节点 // ShowLine显示节点间连线 // ShowPlusMinus: 显示加减号 // ExpandAll();全部展开 TreeNoderoot1=treeView1.Nodes.Add("公司部门");//增加根 节点1 TreeNoderoot2=treeView1.Nodes.Add("学校部门");//增加根 节点2 root1.Nodes.Add("商务部");//为根节点1加三个子节点 root1.Nodes.Add("技术部"); root1.Nodes.Add("市场部"); root2.Nodes.Add("组织部"); root2.Nodes.Add("科研处"); root2.Nodes.Add("教务处"); treeView1.ShowLines=true; treeView1.ShowPlusMinus=true; treeView1.ExpandAll(); } privatevoidbutton1_Click(objectsender,EventArgse) { stringstr=listBox1.SelectedItem.ToString(); listBox2.Items.Add(str); } privatevoidbutton2_Click(objectsender,EventArgse) { stringstr=listBox2.SelectedItem.ToString(); listBox1.Items.Add(str); } //进度条的使用: 控件的最大最小值分别由: Maximum和Minmum决定, 而其增长值是由Value决定,一小格由Step(步长)决定,下面是和Timer一起使 用 privatevoidtimer1_Tick(objectsender,EventArgse) { progressBar1.Value++; if(progressBar1.Value>99) { progressBar1.Value=99; } } } } //制作类似于QQ最小化的托盘控件Notify(最小项)+ContextMenu(菜 单项) privatevoidForm1_SizeChanged(objectsender,EventArgse) { if(this.WindowState==FormWindowState.Minimized) { this.Hide(); this.notifyIcon1.Visible=true;//隐身当前窗体页面变为 状态栏上图标 } } privatevoidnotifyIcon1_Click(objectsender,EventArgse) { //this.Visible=true; //this.WindowState=FormWindowState.Normal; //contextMenuStrip1.Show(); } privatevoidnotifyIcon1_MouseClick(objectsender, MouseEventArgse) { contextMenuStrip1.Show();//显示ContextMenuItems } //需设置Form窗体的ContextScript的属性为: contextMenuStrip1 //容器类 1)FlowLayoutPanel: 流布局控件(复杂窗体) 属性: FlowDirection: 布局方向(左右或上下) WrapContents: 设置是换行还是剪裁 FlowBreak: 是否在流方向上布局并换行: 是其的子控件的属性: 摆 脱FlowDirection的约束 2)分隔条控件SplitContainer: 可以看做是由一个可移动的拆粉条和其分隔的两 个面板组成,并可以含该SplitContainter控件 属性: 分隔部分方向的设置: Orientation: 方向设置: 水平或垂直 IsSplitterFixed: 是否将设置两面板的大小不变=lock 3)TabControl: 选项卡控件: 显示抖个选项卡 更多选项可以右击“添加选项卡” //菜单和工具栏 1)快捷键菜单: ContextMenuStrip: 右击弹出式菜单,需要在所需右击的控件 的ContextMenuScript的属性上设置为: ContextMenuScript属性 2)主菜单: MenuStrip 3)状态栏: StatusStrip 4)工具栏: ToolStrip 40;;//组件: 实际上是一种封装了的对象 1)帮助文档组件HelpProvide: 打开帮助文档,或指定到特点文档 privatevoidForm1_Load(objectsender,EventArgse) { //1)帮助文档组件HelpProvide: 打开帮助文档,或指定到特点文 档 // 属性: tag和HelpNamespace(用于指定HelpProvide控件关联 的帮助文件名称) // 方法: GetHelpString: 设置弹出窗口容 // SetHelpKeyword: 设置控件的相关联字符串 // SetHelpString: 设置控件的相连字符串 // SetshowHelp: 设置是否显示控件的帮助信息 stringstrPath=Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\")).LastIndexOf("\"));//取得本 应用程序可执行文件路径名不包括文件名 strPath+="\help.html";
");
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 基础知识 学习 笔记
![提示](https://static.bdocx.com/images/bang_tan.gif)