C#入门小程序ZHP_06.doc_第1页
C#入门小程序ZHP_06.doc_第2页
C#入门小程序ZHP_06.doc_第3页
C#入门小程序ZHP_06.doc_第4页
C#入门小程序ZHP_06.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C#入门小程序集锦六(2则)这个集锦来源于我开始接触C#时所分析过的一些小程序和一些自己设计的小程序,我是从.NET Framework v2.0 SDK开始的,所以一开始的程序各个.NET版本都能够运行。提醒:一是编程环境往往具有向下兼容性,所以前面程序中的某些注释说明在.NET2.0中是正确的,而在v4.0等版本却是错误的,例如_03中“接口的属性或索引器如下:必须带有至少一个访问器”在.NET4.0中就可以不带访问器;二是这个集锦的作用并不是帮助学习,而是为了总结和查询的,所以内容上并不存在连续性,程序时简时杂,注释也不是为了说明,而是为了提醒。综上所述,请各位按自己的需要查看集锦文档。异步操作:/应用知识:WebRequest、WebResponse、BeginGetResponse()和BeginRead()异步操作/个人理解:还是不太懂异步操作的有什么特别的意义。using System;using System.Net;using System.IO;using System.Text;using System.Threading;public class RequestState const int BUFFER_SIZE = 1024; public StringBuilder requestData; public byte bufferRead; public WebRequest request; public WebResponse response; public Stream responseStream; public RequestState() bufferRead = new byteBUFFER_SIZE; requestData = new StringBuilder(); request = null; responseStream = null; class WebRequest_BeginGetResponse public static ManualResetEvent allDone= new ManualResetEvent(false); const int BUFFER_SIZE = 1024; static void Main() try WebRequest myWebRequest= WebRequest.Create();/发布请求 /WebProxy proxy=new WebProxy(myproxy:80);/建立代理服务器的项目 myWebRequest.Credentials=new NetworkCredential(qingfenghen101,19900322241);/设置代理服务器的身份验证 /myWebRequest.Proxy=proxy;/设置请求的代理 RequestState myRequestState = new RequestState();/建立管理的项目 myRequestState.request = myWebRequest;/设置管理的接口:请求 IAsyncResult asyncResult=(IAsyncResult) myWebRequest.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);/发出异步请求 allDone.WaitOne();/阻止当前线程,直到当前的WaitHandle收到信号 myRequestState.response.Close(); Console.Read(); catch(WebException e) Console.WriteLine(WebException raised!); Console.WriteLine(n0,e.Message); Console.WriteLine(n0,e.Status); catch(Exception e) Console.WriteLine(Exception raised!); Console.WriteLine(Source : + e.Source); Console.WriteLine(Message : + e.Message); private static void RespCallback(IAsyncResult asynchronousResult) try RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;/获取用户自定义对象所包含的异步信息 WebRequest myWebRequest1=myRequestState.request;/使用相应的WebRequest异步信息 myRequestState.response = myWebRequest1.EndGetResponse(asynchronousResult);/请求响应的WebResponse Stream responseStream = myRequestState.response.GetResponseStream(); myRequestState.responseStream=responseStream; IAsyncResult asynchronousResultRead = responseStream.BeginRead(myRequestState.bufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);/开始异步读操作 catch(WebException e) Console.WriteLine(WebException raised!); Console.WriteLine(n0,e.Message); Console.WriteLine(n0,e.Status); catch(Exception e) Console.WriteLine(Exception raised!); Console.WriteLine(Source : + e.Source); Console.WriteLine(Message : + e.Message); private static void ReadCallBack(IAsyncResult asyncResult) try RequestState myRequestState = (RequestState)asyncResult.AsyncState; Stream responseStream = myRequestState.responseStream; int read = responseStream.EndRead( asyncResult );/等待异步读取结束,返回字节数 if (read 0) myRequestState.requestData.Append(Encoding.ASCII.GetString(myRequestState.bufferRead, 0, read);/将bufferRead中读取到的字节转化为string IAsyncResult asynchronousResult = responseStream.BeginRead( myRequestState.bufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState); else Console.WriteLine(nThe HTML page Contents are: ); if(myRequestState.requestData.Length1) string sringContent; sringContent = myRequestState.requestData.ToString(); Console.WriteLine(sringContent); string path = D:mytext.txt; using (StreamWriter sw = File.CreateText(path) sw.Write(sringContent); Console.WriteLine(nPress Enter key to continue.); responseStream.Close(); allDone.Set(); catch(WebException e) Console.WriteLine(WebException raised!); Console.WriteLine(n0,e.Message); Console.WriteLine(n0,e.Status); catch(Exception e) Console.WriteLine(Exception raised!); Console.WriteLine(Source : 0 , e.Source); Console.WriteLine(Message : 0 , e.Message); 控制Windows窗口:/ C# Document/本程序是将桌面打开的test-记事本文件的内容设置为name/知识要点:(C/C+)API的static方法的引用(本节引用的是Windows支持的API应用)/扩展:鼠标模拟using System;using System.Runtime.InteropServices;namespace ZHP.Testclass Testconst int WM_SETTEXT = 0x000C;DllImport(user32.dll)public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);DllImport(user32.dll)public static extern IntPtr FindWindowEx(IntPtr parent,IntPtr child,string strClass,string strText);DllImport(user32.dll)public static extern int SendMessage(IntPtr hwnd,int Msg,IntPtr wParam,string lParam);static void Main(string args)IntPtr htext = FindWin

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论