分布式程序
程序分布在多个服务器,数据来至多个服务器
面向服务的架构
服务提供者把服务提供给中间人,然后中间人向客户服务
WebServices:夸网络提供服务的集合
Web服务优点:基于标准,非商业性,简便性,独立于语言和平台,功能抽象,可发现性,提高开发效率
Web服务标准
Soap(简单对象协议):基于xml的简单访问协议,实现远程调用
UDDI(统一描述发现和集成):为web服务提供信息注册的实现标准规范
WSDL(web服务描述语言):xml描述的web服务接口
编写website
创建工程,web服务
[WebMethod]
public int NumAdd(int a,int b)
{
return a + b;
}
在每个方法前加上[WebMethod]
然后在写方法,方法的权限为公开,且有返回值
添加web引用到项目中
在项目中引用web服务
创建全局对象
在项目中web服务的位置.Service s=new 在项目中web服务的位置.Service();
然后在需要的位置使用对象调用方法
用户调用web服务,中间产生了一个代理类,使用代理类即可
操作线程进度条(委托事件)
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 System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//声明委托
delegate void MyDelegate(int i);
//将委托设置为空
MyDelegate md = null;
//声明线程
Thread thread;
void ShowMessage(int i)
{
this.progressBar1.Value = i;
this.textBox1.Text = i.ToString();
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
md = new MyDelegate(ShowMessage);
thread = new Thread(MyEvent);
thread.IsBackground = true;
//IsBackground 是否后台
//这个属性很重要 .如果 Thread IsBackground 等于false
// 当线程还没有结束时,你点了关闭按钮
// 将抛出An unhandled exception
//of type 'System.InvalidOperationException'
//occurred in System.Windows.Forms.dll 异常
thread.Start();
}
public void MyEvent()
{
for (int i = 0; i < 100;i++ )
{
Thread.Sleep(500);
//线程异步执行(同时执行)
this.BeginInvoke(md,new object[]{i});
}
}
private void button2_Click(object sender, EventArgs e)
{
//线程暂停
thread.Suspend();
}
private void button4_Click(object sender, EventArgs e)
{
//恢复线程
thread.Resume();
}
private void button3_Click(object sender, EventArgs e)
{
//终止线程
if (thread.IsAlive)
{
thread.Abort();
}
}
}
}
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 System.Threading;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
delegate void MyDelegate(string str);
MyDelegate md = null;
Thread th = null;
private void btnStart_Click(object sender, EventArgs e)
{
md = new MyDelegate(ShowMessage);
th = new Thread(Event);
th.IsBackground=true;
th.Start();
}
void ShowMessage(string str)
{
tbShow.Text= str;
}
void Event()
{
FileStream fs = new FileStream(@"E:/培训/3.30/list.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
List<string> list = new List<string>();
string s = null;
do
{
s=sr.ReadLine();
list.Add(s);
}while(s!=null);
while(true)
{
Thread.Sleep(10);
Random rd = new Random();
int i = rd.Next(10);
this.BeginInvoke(md, new object[] { list[i]});
}
sr.Close();
fs.Close();
}
private void btnSuspend_Click(object sender, EventArgs e)
{
th.Suspend();
}
private void btnResume_Click(object sender, EventArgs e)
{
th.Resume();
}
}
}
文件操作(字节数据)
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 System.IO;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//使用FileStream操作二进制文件,二进制文件使用字节存储
FileStream stream = new FileStream(@"E:/a.ini", FileMode.Create, FileAccess.Write);
//将整数转换成字节
byte[] bytes = BitConverter.GetBytes(Convert.ToInt32(textBox1.Text.Trim()));
//向文件中写入字节数组,参数1表示字节数组,参数2表示从数组的哪个地方开始,参数3表示写入的长度
stream.Write(bytes, 0, bytes.Length);
stream.Close();
MessageBox.Show("成功!");
}
private void button2_Click(object sender, EventArgs e)
{
FileStream stream = new FileStream(@"E:/a.ini",FileMode.Open,FileAccess.Read);
byte[] b=new byte[stream.Length];
stream.Read(b, 0,(int)stream.Length);
//字节转换成整数,BitConverter.ToInt32(参数1,参数2)
//参数1,表示字节数组,参数2表示从第几个字节开始,注意基本数据的字节数
//比如说,从第一个字节开始时索引值为0,那么第二个数是从索引为4开始读取
textBox12.Text = BitConverter.ToInt32(b,0).ToString();
stream.Close();
}
}
}
读取mp3文件信息
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 System.IO;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string strPath = string.Empty;
private void button1_Click(object sender, EventArgs e)
{
//OpenFileDialog控件
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
//将控件中选中的文件路径取出
strPath= ofd.FileName;
textBox1.Text = ofd.FileName;
//创建文件流
FileStream fs = new FileStream(strPath,FileMode.Open,FileAccess.Read);
//为mp3文件创建的字节数组
byte[] Tag =new byte[3];
byte[] Title=new byte[30];
byte[] Artist = new byte[30];
byte[] Album = new byte[30];
byte[] Year = new byte[4];
byte[] Comment=new byte[28];
//将文件定位到后128位
fs.Seek(-128, SeekOrigin.End);
//将文件读到字节数组中
fs.Read(Tag,0,3);
fs.Read(Title,0,30);
fs.Read(Artist,0,30);
fs.Read(Album,0,30);
fs.Read(Year,0,4);
fs.Read(Comment,0,28);
//将字节中的信息取出
if(Encoding.Default.GetString(Tag).Equals("TAG"))
{
textBox2.Text=Encoding.Default.GetString(Title);
textBox3.Text=Encoding.Default.GetString(Artist);
textBox4.Text=Encoding.Default.GetString(Album);
textBox5.Text=Encoding.Default.GetString(Year);
textBox7.Text=Encoding.Default.GetString(Comment);
}
}
}
}
using System;
using System.IO;
using System.Text;
namespace TagEditor
{
public class Mp3Info
{
#region Fields
privatestring pFilePath;
privatestring pTAGID;
privatestring pTitle;
privatestring pArtist;
privatestring pAlbum;
privatestring pYear;
privatestring pComment;
privatestring pGenre;
#endregion
#region Properties
publicstring Title
{
get { return pTitle; }
set { pTitle = value; } } publicstring Artist
{
get { return pArtist; }
set { pArtist = value; }
}
publicstring Album
{
get { return pAlbum; }
set { pAlbum = value; }
}
publicstring Year
{
get { return pYear; }
set { pYear = value; }
}
publicstring Comment { get { return pComment; }
set { pComment = value; }
}
publicstring Genre
{
get { return pGenre; }
set { pGenre = value; }
}
#endregion
#region Constructor(s)
public Mp3Info(string file)
{
pFilePath = file;
}
#endregion
#region Public Methods
publicvoid Read()
{
if (!File.Exists(pFilePath))
{
throw new Exception("File: /""+ pFilePath +"/" doen't exists");
}
using (FileStream fs = File.OpenRead(pFilePath))
{
if (fs.Length < 128) { return; }
ID3Tag tag =new ID3Tag();
fs.Seek(-128, SeekOrigin.End);
fs.Read(tag.TAGID, 0, tag.TAGID.Length);
fs.Read(tag.Title, 0, tag.Title.Length);
fs.Read(tag.Artist, 0, tag.Artist.Length);
fs.Read(tag.Album, 0, tag.Album.Length);
fs.Read(tag.Year, 0, tag.Year.Length);
fs.Read(tag.Comment, 0, tag.Comment.Length);
fs.Read(tag.Genre, 0, tag.Genre.Length);
pTAGID = Encoding.Default.GetString(tag.TAGID);
if (pTAGID.Equals("TAG"))
{
pTitle = Encoding.Default.GetString(tag.Title);
pArtist = Encoding.Default.GetString(tag.Artist);
pAlbum = Encoding.Default.GetString(tag.Album);
pYear = Encoding.Default.GetString(tag.Year);
pComment = Encoding.Default.GetString(tag.Comment);
pGenre = Encoding.Default.GetString(tag.Genre);
}
}
}
publicvoid Save()
{
if (!File.Exists(pFilePath))
{
throw new Exception("File: /""+ pFilePath +"/" doen't exists");
}
using (FileStream fs = File.OpenWrite(pFilePath))
{
ID3Tag tag =new ID3Tag();
Encoding.Default.GetBytes("TAG").CopyTo(tag.TAGID, 0);
Encoding.Default.GetBytes(pTitle).CopyTo(tag.Title, 0);
Encoding.Default.GetBytes(pArtist).CopyTo(tag.Artist, 0);
Encoding.Default.GetBytes(pAlbum).CopyTo(tag.Album, 0);
Encoding.Default.GetBytes(pYear).CopyTo(tag.Year, 0);
Encoding.Default.GetBytes(pComment).CopyTo(tag.Comment, 0);
tag.Genre[0] = Convert.ToByte(pGenre);
fs.Seek(-128, SeekOrigin.End);
fs.Write(tag.TAGID, 0, tag.TAGID.Length);
fs.Write(tag.Title, 0, tag.Title.Length);
fs.Write(tag.Artist, 0, tag.Artist.Length);
fs.Write(tag.Album, 0, tag.Album.Length);
fs.Write(tag.Year, 0, tag.Year.Length);
fs.Write(tag.Comment, 0, tag.Comment.Length);
fs.Write(tag.Genre, 0, tag.Genre.Length);
}
}
#endregion
}
class ID3Tag
{
public byte[] TAGID =new byte[3]; // 3, 必须是TAG
public byte[] Title =new byte[30]; // 30, 歌曲的标题
public byte[] Artist =new byte[30]; // 30, 歌曲的艺术家
public byte[] Album =new byte[30]; // 30, 专辑名称
public byte[] Year =new byte[4]; // 4, 出版年代
public byte[] Comment =new byte[30]; // 30, 评论
public byte[] Genre =new byte[1]; // 1, 种类标识
}
}