-
WPF MVVM登陆实例
WPF MVVM登陆实例
- 2014-12-17下载
- 积分:1
-
C语言函数大全(语法着色)html版本
C语言函数大全(语法着色)html版本
- 2021-05-06下载
- 积分:1
-
C# 台达PLC485通信实例
【实例简介】
- 2021-08-23 00:31:05下载
- 积分:1
-
ADSL宽带拨号 (C#)实例源码下载
实例截图: 部分源码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Lqarpjj.Study.Demo.ADLSTool.Class;using System.Diagnostics;using System.IO;using Microsoft.Win32;namespace Lqarpjj.Study.Demo.ADLSTool{ public partial class frmMain : Form { #region 自定义变量 /// /// 静态变量存储信息的地址文件 /// private static string ADSLPath = Application.StartupPath @"ADSL.ini"; private static string ConnectionsPath = Application.StartupPath @"create.vbs"; /// /// 定义Ini写入对象 /// private IniWrite ini = new IniWrite(ADSLPath); /// /// 判断第一登录 /// private bool IsFirst = true; /// /// /// private MD5.DES.Md5Class md5 = new MD5.DES.Md5Class(); /// /// /// public static string key = null; #endregion /// /// 构造函数 /// public frmMain() { InitializeComponent(); } #region 窗体事件 /// /// 窗体初始 /// /// /// private void frmMain_Load(object sender, EventArgs e) { SetToolTip(); IntiLoad(sender, e); } #endregion #region 控件按钮事件 /// /// 复制账号 /// /// /// private void btnCopyAccount_Click(object sender, EventArgs e) { if (this.chkSpecial.Checked)//判断特殊拨号是否勾选 { //进行加密 UsernameEncode encode = new UsernameEncode(); Clipboard.SetDataObject(encode.EncodeUsername(this.txtAccount.Text, this.txtPassword.Text)); } else { Clipboard.SetDataObject(this.txtAccount.Text); } MessageBox.Show("账号已经复制到剪贴板!", "复制成功"); } /// /// 拨号 /// /// /// private void btnDia_Click(object sender, EventArgs e) { if (this.txtAccount.Text.Trim() == "") { MessageBox.Show("账号不能为空", "输入错误"); txtAccount.Focus(); return; } if (this.txtPassword.Text.Trim() == "") { MessageBox.Show("密码不能为空", "输入错误"); txtPassword.Focus(); return; } this.NetDia(); } /// /// 找不到电话簿? /// /// /// private void lblNotFind_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { this.CreateConnections(); } /// /// 记住账号联动 /// /// /// private void chkRemAccount_CheckedChanged(object sender, EventArgs e) { if (!this.chkRemAccount.Checked) { this.chkRemPassword.Checked = false; } } /// /// 记住密码联动 /// /// /// private void chkRemPassword_CheckedChanged(object sender, EventArgs e) { if (this.chkRemPassword.Checked) { this.chkRemAccount.Checked = true; } } /// /// 自动登录启动 /// /// /// private void AutoConectTimer_Tick(object sender, EventArgs e) { this.AutoConectTimer.Enabled = false; new frmLinkInfo(this).Show(); base.Hide(); } #endregion #region 自定义方法 /// /// 初始登录 /// /// /// private void IntiLoad(object sender, EventArgs e) { FileInfo info = new FileInfo(ADSLPath); try { if (info.Exists) { this.ReadConfig(); } if (this.chkAutoDia.Checked)//是否自动登录 { this.AutoConectTimer.Start(); } } catch (Exception) { File.Delete(ADSLPath); this.frmMain_Load(sender, e); } } /// ///设置控件提示 /// private void SetToolTip() { ToolTip tip = new ToolTip(); tip.BackColor = System.Drawing.Color.Transparent; tip.SetToolTip(this.btnCopyAccount, "复制账号到剪贴板。如果勾选了特殊拨号,复制的则是加密过的账号。"); tip.SetToolTip(this.btnDia, "手动进行拨号。"); tip.SetToolTip(this.chkAutoDia, "勾选后,下次启动将自动拨号。"); tip.SetToolTip(this.chkSpecial, "如果你原本使用协同拨号器拨号,请勾选此项。"); tip.SetToolTip(this.chkRemPassword, "勾选后,将会保存密码信息,同时保存账号信息。"); tip.SetToolTip(this.chkRemAccount, "勾选后,将会保存账号信息,但不会保存密码信息。"); tip.SetToolTip(this.txtPassword, "在这里输入你的密码。如果有字母,有大小写之分。"); tip.SetToolTip(this.lblNotFind, "如果拨号时提示找不到电话簿,请点击这里。"); tip.SetToolTip(this.txtAccount, "在这里输入你的用户名,如果是特殊拨号不分大小写。"); tip.SetToolTip(this.chkAutoClo, "勾选后拨号成功将自动关闭程序,如果拨号失败,则不会自动关闭。"); } /// /// 写入配置信息 /// private void WriteConfig() { this.ini.Writue("用户设置", "记住账号", this.chkRemAccount.Checked.ToString()); this.ini.Writue("用户设置", "记住密码", this.chkRemPassword.Checked.ToString()); this.ini.Writue("用户设置", "特殊拨号", this.chkSpecial.Checked.ToString()); this.ini.Writue("用户设置", "自动连接", this.chkAutoDia.Checked.ToString()); this.ini.Writue("用户设置", "自动关闭", this.chkAutoClo.Checked.ToString()); this.ini.Writue("用户设置", "首次使用", this.IsFirst.ToString()); if (this.chkRemAccount.Checked) { this.ini.Writue("用户信息", "账号", this.md5.Encryption(this.txtAccount.Text)); this.ini.Writue("加密密钥", "账号", key); } if (this.chkRemPassword.Checked) { this.ini.Writue("用户信息", "密码", this.md5.Encryption(this.txtPassword.Text)); this.ini.Writue("加密密钥", "密码", key); } } /// /// 读取配置信息 /// private void ReadConfig() { this.chkRemAccount.Checked = bool.Parse(this.ini.ReadValue("用户设置", "记住账号")); this.chkRemPassword.Checked = bool.Parse(this.ini.ReadValue("用户设置", "记住密码")); this.chkSpecial.Checked = bool.Parse(this.ini.ReadValue("用户设置", "特殊拨号")); this.chkAutoDia.Checked = bool.Parse(this.ini.ReadValue("用户设置", "自动连接")); this.chkAutoClo.Checked = bool.Parse(this.ini.ReadValue("用户设置", "自动关闭")); this.IsFirst = bool.Parse(this.ini.ReadValue("用户设置", "首次使用")); if (this.chkRemAccount.Checked) { this.txtAccount.Text = this.md5.Decryption(this.ini.ReadValue("加密密钥", "账号"), this.ini.ReadValue("用户信息", "账号")); if (this.txtAccount.Text.Trim() == "") { this.txtAccount.Text = null; } } if (this.chkRemPassword.Checked) { this.txtPassword.Text = this.md5.Decryption(this.ini.ReadValue("加密密钥", "密码"), this.ini.ReadValue("用户信息", "密码")); if (this.txtPassword.Text.Trim() == "") { this.txtPassword.Text = null; } } } /// /// 判断是否是第一次使用本软件 /// private void IsFirstUse() { if (this.IsFirst) { if (MessageBox.Show("检测到您是初次使用本软件,是否自动创建一个名为【宽带连接】的连接?如果已经存在,请选否。", "初次使用", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.CreateConnections(); } this.IsFirst = false; this.WriteConfig(); } } /// /// 连接宽带 /// private void NetDia() { this.IsFirstUse(); new frmLinkInfo(this).Show(); base.Hide(); } /// /// 电话簿 /// private void CreateConnections() { Process[] processArray; StreamWriter writer = new StreamWriter(ConnectionsPath, false, Encoding.Default); writer.Write(this.GetVersion()); writer.Close(); Process process = new Process(); process.StartInfo.FileName = ConnectionsPath; process.StartInfo.CreateNoWindow = true; base.Hide(); process.Start(); process.WaitForExit(); File.Delete(ConnectionsPath); Label_006A: processArray = Process.GetProcesses(); foreach (Process process2 in processArray) { if (process2.ProcessName.ToString() == "rasphone") { goto Label_006A; } } base.Show(); } /// /// 获取版本? /// /// private string GetVersion() { StringBuilder str = new StringBuilder(); str.Append(""); str.Append("set ws=createobject("wscript.shell") "); str.Append(" ws.run "rasphone -a 宽带连接" "); str.Append(" wscript.sleep 200 "); str.Append(" ws.sendkeys "{t}" "); str.Append(" wscript.sleep 200 "); str.Append(" ws.sendkeys "{n}" "); str.Append(" wscript.sleep 200 "); str.Append(" ws.sendkeys "{enter}" "); str.Append(" wscript.sleep 200 "); str.Append(" ws.sendkeys "{enter}" "); if (Registry.LocalMachine.OpenSubKey(@"SoftwareMicrosoftWindows NTCurrentVersion").GetValue("ProductName").ToString() == "Windows 7 Ultimate") { str.Append(""); str.Append("set ws=createobject("wscript.shell")"); str.Append(" ws.run "rasphone -a 宽带连接""); str.Append(" wscript.sleep 200"); str.Append(" ws.sendkeys "{enter}""); str.Append(" wscript.sleep 200"); str.Append(" ws.sendkeys "{enter}""); } return str.ToString(); } #endregion }}
- 2014-07-16下载
- 积分:1
-
C# 套打Demo
套打Demo 【核心代码】using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Drawing.Printing;namespace Print_Demo{ public partial class Form1 : Form { public PrintDocument printDt = new PrintDocument(); //打印文档对象 Font printFont; //打印使用的字体 public Form1() { InitializeComponent(); } void printDt_PrintPage(object sender, PrintPageEventArgs e) { float pointX = 10; float pointY = 10; e.Graphics.DrawString("打印内容", new Font("宋体", 16F), Brushes.Black, pointX 60, pointY); e.Graphics.DrawLine(new Pen(Color.Black), pointX, pointY 26,pointX 300,pointY 26); e.Graphics.DrawString("打印内容", printFont, Brushes.Black, pointX, pointY 35); e.Graphics.DrawLine(new Pen(Color.Black), pointX, pointY 56, pointX 300, pointY 56); e.Graphics.DrawString("打印内容", printFont, Brushes.Black, pointX, pointY 65); e.Graphics.DrawLine(new Pen(Color.Black), pointX, pointY 86, pointX 300, pointY 86); e.Graphics.DrawString("打印内容", printFont, Brushes.Black, pointX, pointY 95); e.Graphics.DrawLine(new Pen(Color.Black), pointX, pointY 116, pointX 300, pointY 116); e.Graphics.DrawString("打印内容", printFont, Brushes.Black, pointX, pointY 125); e.Graphics.DrawLine(new Pen(Color.Black), pointX, pointY 146, pointX 300, pointY 146); e.Graphics.DrawString("打印内容", printFont, Brushes.Black, pointX, pointY 155); e.Graphics.DrawLine(new Pen(Color.Black), pointX, pointY 176, pointX 300, pointY 176); e.Graphics.DrawString("打印内容", printFont, Brushes.Black, pointX, pointY 185); e.Graphics.DrawLine(new Pen(Color.Black), pointX, pointY 206, pointX 300, pointY 206); e.Graphics.DrawString("检测结果", printFont, Brushes.Black, pointX, pointY 233); e.Graphics.DrawString("通过", new Font("宋体",22F,FontStyle.Bold), Brushes.Black, pointX 90, pointY 225); e.Graphics.DrawLine(new Pen(Color.Black), pointX, pointY 265, pointX 300, pointY 265); e.Graphics.DrawString("单位名称", printFont, Brushes.Black, pointX, pointY 275); } private void Form1_Load(object sender, EventArgs e) { printDt.PrintPage = new PrintPageEventHandler(printDt_PrintPage); printFont = new Font("宋体", 12F); } private void btnPrint_Click(object sender, EventArgs e) { //PrintDialog printDlg = new PrintDialog(); //printDlg.Document = printDt; //printDlg.AllowPrintToFile = true; //printDlg.AllowCurrentPage = true; //printDlg.AllowSelection = true; //printDlg.ShowDialog(); //printDlg.ShowDialog(); printDt.Print(); } private void btnLook_Click(object sender, EventArgs e) { PrintPreviewDialog printPreview = new PrintPreviewDialog(); printPreview.PrintPreviewControl.Document = printDt; printPreview.ShowDialog(this); } }}
- 2021-05-06下载
- 积分:1
-
c# 网络编程 实例源码(pdf)
我们知道 C#和 C++的差异之一,就是他本身没有类库,所使用的类库是.Net 框架中的类库--.Net FrameWork SDK。在.Net FrameWork SDK 中为网络编程提供了二个名称空间:"System.Net"和"System.Net.Sockets"。C#就是通过这二个名称空间中封装的类和方法实现网络通讯的。 首先我们解释一下在网络编程时候,经常遇到的几个概念:同步(synchronous)、异步(asynchronous)、阻塞(Block)和非阻塞(Unblock): 所谓同步方式,就是发送方发送数据包以后,丌等接受方响应,就接着发送下一个数据包。异步方式就是当发送方发送一个数据包以后,一直等到接受方响应后,才接着发送下一个数据包。而阻塞套接字是指执行此套接字的网络调用时,直到调用成功才返回,否则此套节字就一直阻塞在网络调用上,比如调用 StreamReader 类的 Readlin ( )方法读取网络缓冲区中的数据,如果调用的时候没有数据到达,那么此 Readlin ( )方法将一直挂在调用上,直到读到一些数据,此函数调用才返回;而非阻塞套接字是指在执行此套接字的网络调用时,丌管是否执行成功,都立即返回。同样调用 StreamReader 类的 Readlin ( )方法读取网络缓冲区中数据,丌管是否读到数据都立即返回,而丌会一直挂在此函数调用上。在 Windows网络通信软件开发中,最为常用的方法就是异步非阻塞套接字。平常所说的 C/S(客户端/服务器)结构的软件采用的方式就是异步非阻塞模式的。 其实在用 C#迚行网络编程中,我们并丌需要了解什么同步、异步、阻塞和非阻塞的原理和工作机制,因为在.Net FrameWrok SDK 中已经已经把这些机制给封装好了。下面我们就用 C#开一个具体的网络程序来说明一下问题。 一.本文中介绍的程序设计及运行环境 (1).微软视窗 2000 服务器版 (2)..Net Framework SDK Beta 2 以上版本 二.服务器端程序设计的关键步骤以及解决办法: 在下面接受的程序中,我们采用的是异步阻塞的方式。 (1).首先要要在给定的端口上面创建一个"tcpListener"对象侦听网络上面的请求。当接收到连结请求后通过调用"tcpListener"对象的"AcceptSocket"方法产生一个用于处理接入连接请求的 Socket 的实例。下面是具体实现代码: //创建一个 tcpListener 对象,此对象主要是对给定端口迚行侦听 tcpListener = new TcpListener ( 1234 ) ; //开始侦听 tcpListener.Start ( ) ; //返回可以用以处理连接的 Socket 实例 socketForClient = tcpListener.AcceptSocket ( ) ; (2).接受和发送客户端数据: 此时 Socket 实例已经产生,如果网络上有请求,在请求通过以后,Socket 实例构造一个"NetworkStream"对象,"NetworkStream"对象为网络访问提供了基础数据流。我们通过名称空间"System.IO"中封装的二个类"StreamReader"和"StreamWriter"来实现对"NetworkStream"对象的访问。其中"StreamReader"类中的 ReadLine ( )方法就是从"NetworkStream"对象中读取一行字符;"StreamWriter"类中的 WriteLine ( )方法就是对"NetworkStream"对象中写入一行字符串。从而实现在网络上面传输字符串,下面是具体的实现代码: try { //如果返回值是"true",则产生的套节字已经接受来自进方的连接请求 if ( socketForClient.Connected ) { ListBox1.Items.Add ( "已经和客户端成功连接!" ) ; while ( true ) { //创建 networkStream 对象通过网络套节字来接受和发送数据 networkStream = new NetworkStream ( socketForClient ) ; //从当前数据流中读取一行字符,返回值是字符串 streamReader = new StreamReader ( networkStream ) ; string msg = streamReader.ReadLine ( ) ; ListBox1.Items.Add ( "收到客户端信息:" msg ) ; streamWriter = new StreamWriter ( networkStream ) ; if ( textBox1.Text != "" ) { ListBox1.Items.Add ( "往客户端反馈信息:" textBox1.Text ) ; //往当前的数据流中写入一行字符串 streamWriter.WriteLine ( textBox1.Text ) ; //刷新当前数据流中的数据 streamWriter.Flush ( ) ; } } } } catch ( Exception ey ) { MessageBox.Show ( ey.ToString ( ) ) ; } (3).最后别忘了要关闭所以流,停止侦听网络,关闭套节字,具体如下: //关闭线程和流 networkStream.Close ( ) ; streamReader.Close ( ) ; streamWriter.Close ( ) ; _thread1.Abort ( ) ; tcpListener.Stop ( ) ; socketForClient.Shutdown ( SocketShutdown.Both ) ; socketForClient.Close ( ) ; 三.C#网络编程服务器端程序的部分源代码(server.cs): 由于在此次程序中我们采用的结构是异步阻塞方式,所以在实际的程序中,为了丌影响服务器端程序的运行速度,我们在程序中设计了一个线程,使得对网络请求侦听,接受和发送数据都在线程中处理,请在下面的代码中注意这一点,下面是 server.cs 的完整代码: using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Net.Sockets ; using System.IO ; using System.Threading ; using System.Net ; //导入程序中使用到的名字空间 public class Form1 : Form { private ListBox ListBox1 ; private Button button2 ; private Label label1 ; private TextBox textBox1 ; private Button button1 ; private Socket socketForClient ; private NetworkStream networkStream ; private TcpListener tcpListener ; private StreamWriter streamWriter ; private StreamReader streamReader ; private Thread _thread1 ; private System.ComponentModel.Container components = null ; public Form1 ( ) { InitializeComponent ( ) ; } //清除程序中使用的各种资源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } private void InitializeComponent ( ) { label1 = new Label ( ) ; button2 = new Button ( ) ; button1 = new Button ( ) ; ListBox1 = new ListBox ( ) ; textBox1 = new TextBox ( ) ; SuspendLayout ( ) ; label1.Location = new Point ( 8 , 168 ) ; label1.Name = "label1" ; label1.Size = new Size ( 120 , 23 ) ; label1.TabIndex = 3 ; label1.Text = "往客户端反馈信息:" ; //同样的方式设置其他控件,这里略去 this.Controls.Add ( button1 ) ; this.Controls.Add ( textBox1 ) ; this.Controls.Add ( label1 ) ; this.Controls.Add ( button2 ) ; this.Controls.Add ( ListBox1 ) ; this.MaximizeBox = false ; this.MinimizeBox = false ; this.Name = "Form1" ; this.Text = "C#的网络编程服务器端!" ; this.Closed = new System.EventHandler ( this.Form1_Closed ) ; this.ResumeLayout ( false ) ; } private void Listen ( ) { //创建一个 tcpListener 对象,此对象主要是对给定端口迚行侦听 tcpListener = new TcpListener ( 1234 ) ; //开始侦听 tcpListener.Start ( ) ; //返回可以用以处理连接的 Socket 实例 socketForClient = tcpListener.AcceptSocket ( ) ; try { //如果返回值是"true",则产生的套节字已经接受来自进方的连接请求 if ( socketForClient.Connected ) { ListBox1.Items.Add ( "已经和客户端成功连接!" ) ; while ( true ) { //创建 networkStream 对象通过网络套节字来接受和发送数据 networkStream = new NetworkStream ( socketForClient ) ; //从当前数据流中读取一行字符,返回值是字符串 streamReader = new StreamReader ( networkStream ) ; string msg = streamReader.ReadLine ( ) ; ListBox1.Items.Add ( "收到客户端信息:" msg ) ; streamWriter = new StreamWriter ( networkStream ) ; if ( textBox1.Text != "" ) { ListBox1.Items.Add ( "往客户端反馈信息:" textBox1.Text ) ; //往当前的数据流中写入一行字符串 streamWriter.WriteLine ( textBox1.Text ) ; //刷新当前数据流中的数据 streamWriter.Flush ( ) ; } } } } catch ( Exception ey ) { MessageBox.Show ( ey.ToString ( ) ) ; } } static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } private void button1_Click ( object sender , System.EventArgs e ) { ListBox1.Items .Add ( "服务已经启动!" ) ; _thread1 = new Thread ( new ThreadStart ( Listen ) ) ; _thread1.Start ( ) ; } private void button2_Click ( object sender , System.EventArgs e ) { //关闭线程和流 networkStream.Close ( ) ; streamReader.Close ( ) ; streamWriter.Close ( ) ; _thread1.Abort ( ) ; tcpListener.Stop ( ) ; socketForClient.Shutdown ( SocketShutdown.Both ) ; socketForClient.Close ( ) ; } private void Form1_Closed ( object sender , System.EventArgs e ) { //关闭线程和流 networkStream.Close ( ) ; streamReader.Close ( ) ; streamWriter.Close ( ) ; _thread1.Abort ( ) ; tcpListener.Stop ( ) ; socketForClient.Shutdown ( SocketShutdown.Both ) ; socketForClient.Close ( ) ; } } 四.客户端程序设计的关键步骤以及解决办法: (1).连接到服务器端的指定端口: 我们采用的本地机既做服务器也做客户机,你可以通过修改 IP 地址来确定自己想要连接的服务器。我们在连接的时候采用了"TcpClient"类,此类是在较高的抽象级别(高于Socket 类)上面提供 TCP 服务。下面代码就是连接到本地机(端口为 1234),并获取响应流: //连接到服务器端口,在这里是选用本地机器作为服务器,你可以通过修改 IP 地址来改变服务器 try { myclient = new TcpClient ( "localhost" , 1234 ) ; } catch { MessageBox.Show ( "没有连接到服务器!" ) ; return ; } //创建 networkStream 对象通过网络套节字来接受和发送数据 networkStream = myclient.GetStream ( ) ; streamReader = new StreamReader ( networkStream ) ; streamWriter = new StreamWriter ( networkStream ) ; (2).实现接受和发送数据: 在接受和发送数据上面,我们依然采用了"NetworkStream"类,因为对他迚行操作比较简单,具体实现发送和接受还是通过命名空间"System.IO"中"StreamReader"类ReadLine ( )方法和"StreamWriter"类的 WriteLine ( )方法。具体的实现方法如下: if ( textBox1.Text == "" ) { MessageBox.Show ( "请确定文本框为非空!" ) ; textBox1.Focus ( ) ; return ; } try { string s ; //往当前的数据流中写入一行字符串 streamWriter.WriteLine ( textBox1.Text ) ; //刷新当前数据流中的数据 streamWriter.Flush ( ) ; //从当前数据流中读取一行字符,返回值是字符串 s = streamReader.ReadLine ( ) ; ListBox1.Items.Add ( "读取服务器端发送内容:" s ) ; } catch ( Exception ee ) { MessageBox.Show ( "从服务器端读取数据出现错误,类型为:" ee.ToString ( ) ) ; } (3).最后一步和服务器端是一样的,就是要关闭程序中创建的流,具体如下: streamReader.Close ( ) ; streamWriter.Close ( ) ; networkStream.Close ( ) ; 五.客户端的部分代码: 由于在客户端丌需要侦听网络,所以在调用上面没有程序阻塞情冴,所以在下面的代码中,我们没有使用到线程,这是和服务器端程序的一个区别的地方。总结上面的这些关键步骤,可以得到一个用 C#网络编程 完整的客户端程序(client.cs),具体如下: using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Net.Sockets ; using System.IO ; using System.Threading ; //导入程序中使用到的名字空间 public class Form1 : Form { private ListBox ListBox1 ; private Label label1 ; private TextBox textBox1 ; private Button button3 ; private NetworkStream networkStream ; private StreamReader streamReader ; private StreamWriter streamWriter ; TcpClient myclient ; private Label label2 ; private System.ComponentModel.Container components = null ; public Form1 ( ) { InitializeComponent ( ) ; } //清除程序中使用的各种资源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } private void InitializeComponent ( ) { label1 = new Label ( ) ; button3 = new Button ( ) ; ListBox1 = new ListBox ( ) ; textBox1 = new TextBox ( ) ; label2 = new Label ( ) ; SuspendLayout ( ) ; label1.Location = new Point ( 8 , 168 ) ; label1.Name = "label1" ; label1.Size = new Size ( 56 , 23 ) ; label1.TabIndex = 3 ; label1.Text = "信息:" ; //同样方法设置其他控件 AutoScaleBaseSize = new Size ( 6 , 14 ) ; ClientSize = new Size ( 424 , 205 ) ; this.Controls.Add ( button3 ) ; this.Controls.Add ( textBox1 ) ; this.Controls.Add ( label1 ) ; this.Controls.Add ( label2 ) ; this.Controls.Add ( ListBox1 ) ; this.MaximizeBox = false ; this.MinimizeBox = false ; this.Name = "Form1" ; this.Text = "C#的网络编程客户器端!" ; this.Closed = new System.EventHandler ( this.Form1_Closed ) ; this.ResumeLayout ( false ) ; //连接到服务器端口,在这里是选用本地机器作为服务器,你可以通过修改 IP 地址来改变服务器 try { myclient = new TcpClient ( "localhost" , 1234 ) ; } catch { MessageBox.Show ( "没有连接到服务器!" ) ; return ; } //创建 networkStream 对象通过网络套节字来接受和发送数据 networkStream = myclient.GetStream ( ) ; streamReader = new StreamReader ( networkStream ) ; streamWriter = new StreamWriter ( networkStream ) ; } static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } private void button3_Click ( object sender , System.EventArgs e ) { if ( textBox1.Text == "" ) { MessageBox.Show ( "请确定文本框为非空!" ) ; textBox1.Focus ( ) ; return ; } try { string s ; //往当前的数据流中写入一行字符串 streamWriter.WriteLine ( textBox1.Text ) ; //刷新当前数据流中的数据 streamWriter.Flush ( ) ; //从当前数据流中读取一行字符,返回值是字符串 s = streamReader.ReadLine ( ) ; ListBox1.Items.Add ( "读取服务器端发送内容:" s ) ; } catch ( Exception ee ) { MessageBox.Show ( "从服务器端读取数据出现错误,类型为:" ee.ToString ( ) ) ; } } private void Form1_Closed ( object sender , System.EventArgs e ) { streamReader.Close ( ) ; streamWriter.Close ( ) ; networkStream.Close ( ) ; } } 下图是编译上面二个程序后运行的界面: 图 01:C#编写网络程序运行界面 七.总结: 虽然在.Net FrameWrok SDK 中只为网络编程提供了二个命名空间,但这二个命名空间中的内容却是十分丰富的,C#利用这二个命名空间既可以实现同步和异步,也可以实现阻塞和非阻塞。本文通过用 C#编写一个网络上信息传输的程序,展现了其丰富的内容,由于篇幅所限,更深,更强大的功能还需要读者去实践、探索。
- 2014-07-07下载
- 积分:1
-
基于MadgwickAHRS算法的3D呈像实现
基于MadgwickAHRS的九轴数据转换为四元数算法的3D呈像图
- 2020-12-11下载
- 积分:1
-
Lucene.Net(全文检索)入门级实例源码下载
.net 全文检索必须了解的
- 2015-03-27下载
- 积分:1
-
C语言入门至精通(全集).pdf【共533页】
【实例简介】基础课程
- 2021-06-10 00:31:03下载
- 积分:1
-
LINQ TO SQLite 完整例子源码下载
前言 当前,软件应用程序中,数据库已经成为不可缺少的重要组成部分.然而传统数据库正趋向巨无霸化,对系统的要求一步步提高,管理成本也越来越大,对于中小型项目的应用,它的很多功能变得越来越多余,但是我却不得不为这些不需要的功能付出更多的资金和人力成本. 在这些场景,嵌入式数据库的轻量,零部署,跨平台,义移植,无疑有着传统数据库无可比拟的优点,当然为此我们必须放弃高容量,高性能,高并发,不过谁在乎这些呢?我们只是中小型应用而已,大小不上百万,并发不超万^_^. 在.Net中,我们可以选择的嵌入式数据库还是挺多的,db4o,sqlite,各种xml db. 我之前更倾向于使用db4o,db4o对于用传统ADO.Net驱动的开发有更多的优势,无需手动Mapping,零SQL.不过整因为db4o做了很多处理,所以在数据持久化的过程中,我们能干预的也很少,而db4o在复合属性,级联属性,特别当整个系统使用富领域模型设计的时候,保存数据的时候往往会出错.为此我们不得不自己手工持久化复合对象中的各个对象,增加关联字段进行关联,这样一来就越加靠近SQL Server之类的传统数据库的时候,显得名不符实.而在web应用中,为了提供并发的能力,还必须使用server/client的模式进行数据传输.总让我感觉有那么点点别扭. 自从DLINQ出来之后,我的项目越来越依赖于它,它让我们的程序更加优雅清晰,再也不用无聊的拼凑一段又一段的sql,编写一遍又遍的配置.可惜的是MS只提供LINQ TO SQL(MS SQL SERVER)的实现. 还好LINQ思想出现之后,社区里涌现了各种各样的LINQ Provider程序,大有一切皆LINQ的架势(LINQ TO Everything),SQLite自然不例外.今天我们进来唠唠如何将SQLite引入我们的开发. ADO.Net Provider&LINQ Provider 首先,下载SQLite的LINQ Provider,在网上我们可以找到两个LINQ Provider For SQLite. 一个是phxsoftware的System.Data.SQLite,另一个是devart的dotConnect for SQLite. System.Data.SQLite是开源产品,而dotConnect For SQLite是商业产品(有个一个免费普通版本).我们这里选择System.Data.SQLite作为我们的LINQ Provider. 从Sourceforge下载SQLite-1.0.60.0-setup.exe,然后安装.结束的时候,会让你Install SQLite Design-Time Support,把里面的VS2005/VS2008 都打上勾 这样子你就可以在VS的Server Explorer里面像管理SQL Server的数据库一样管理你的SQLite数据库文件.安装完之后,我们得到以下文件 创建数据库 要创建一个空的SQLite数据库,可以选择两种方式. 1. 从http://www.sqlite.org/download.html下载Window下的程序(http://www.sqlite.org/sqlite-3_6_19.zip),然后通过命令行创建. .db3后缀名可以任意,你也可以使用其他的任意后缀名. 在sqlite>的shell下,必须输入什么,如果直接.exit退出的话,数据库文件将不会被创建 2.从http://www.sqlite.org/cvstrac/wiki?p=ManagementTools下载各种GUI程序. 数据库文件创建好了之后,拷贝到你想要的位置,然后打开VS的Server Explorer(当然如果你使用GUI程序,你就不需要VS的这个功能了),我们可以在里面维护表信息 我们先设计两个表,以便继续下面的步骤. 创建项目 创建DataContext 当你将表拉入dbml的时候,会得到如下错误"The selected object(s) use an unsupported data provider." dbml designer和DLINQ一样,微软只实现了For SQL SERVER的功能,其他数据库都没实现.既然不能生成dmbl,那我们生成code就好了,反正这个layout我们一般也没什么用.找到从windows sdk目录下找到Code Generation Tool (SqlMetal.exe). 不幸的是,这个东东也只支持MS本家的东西,而且不支持Generator扩展-_-!!!. 既然phxsoftware提供的LINQ Porivder,说不定有提供自定义的代码生成工具.不过看来看去,发现只支持Entity Frameworkd Entity Model的生成,但是没有LINQ TO SQLite的支持(dotConnect for SQLite 倒是对二者都支持). 难道我们要手动编写DataContext?怎么可能,肯定有人实现了,google下,找到了一个DLINQ2007的项目,提供好多LINQ Provider的实现(包括SQLite).发现里面有个DbMetal.exe,支持从SQLite生成DataContext类.虽然不如MS的的完美,不过能自动生成基本的DataContext,这就是最大的收获,不然像我这么依赖LINQ的人只好放弃SQLite了^_^.DbMetal.exe生成DataContext的命令行,类似SqlMetal.exe. DbMetal.exe /database:TestDataContext /schema /namespace=LinqToSQLite /sprocs /entityInterfaces= /culture="en" /code=Test.cs /provider=Sqlite /conn="data source=D:docmydatacodeLinqToSQLitedllsTest.db3" /database指定类名, /namespace=LinqToSQLite指定命名空间...,具体的参数可以在命令行下获取到(dbmetal.exe /?). 不过这个DbMetal.exe一样不完善,不是什么结构都可以生成,比如数据库带有外键的话,生成就会出错. 生成后的代码大概如下(可能需要手工清理下SystemTable的对应的class): public partial class TestDataContext : DbLinq.Data.Linq.DataContext{ public TestDataContext(System.Data.IDbConnection connection): base(connection, new DbLinq.Sqlite.SqliteVendor()){} public TestDataContext(System.Data.IDbConnection connection, DbLinq.Vendor.IVendor vendor): base(connection, vendor){} public Table Order { get { return GetTable(); } } public Table OrderItem { get { return GetTable(); } } }
- 2013-07-20下载
- 积分:1