2008.06.14 / 标签:
C#网络编程 / 分类:
WinForm
这个程序所要实现的功能是:客户端发送一段字符串给服务端,服务端将其字母换成大写形式并返回给客户端。当然这个程序还是为了学习而写的,练练手而已。
客户端源码:
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.IO;using System.Net.Sockets;using System.Net;namespace ClientTest{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; /// <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 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(56, 80); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "发送"; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(48, 8); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 1; this.textBox1.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(48, 40); this.textBox2.Name = "textBox2"; this.textBox2.TabIndex = 2; this.textBox2.Text = ""; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(200, 118); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(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) { //创建tcpclient对象 TcpClient TClient = new TcpClient(); //创建与服务器的连接 TClient.Connect(IPAddress.Parse("127.0.0.1"),8000); //获取网络流 NetworkStream ns = TClient.GetStream(); //创建流写入对象 StreamWriter sw = new StreamWriter(ns); //写入数据 sw.WriteLine(this.textBox1.Text); //清空缓冲 sw.Flush(); //创建流读取对象 StreamReader sr = new StreamReader(ns); //读出数据 this.textBox2.Text=sr.ReadLine(); //关闭 sr.Close(); sw.Close(); ns.Close(); TClient.Close(); } }}[/CODE]Server端源码:[CODE=csharp]using System;using System.IO;using System.Threading;using System.Net;using System.Net.Sockets;namespace ServerTest{ /// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // // TODO: 在此处添加代码以启动应
用程序 // Console.WriteLine("Running....."); Thread thread = new Thread(new ThreadStart(Convert)); thread.Start(); } /// <summary> /// 静态的方法对字符转换 /// </summary> static void Convert() { //创建监听对象并启动 TcpListener TListener = new TcpListener(8000); TListener.Start(); while(true) { //获取socket Socket socket = TListener.AcceptSocket(); //获取网络流 NetworkStream ns = new NetworkStream(socket); //创建流读取对象 StreamReader sr = new StreamReader(ns); //创建流写入对象 StreamWriter sw = new StreamWriter(ns); //读取数据转换大小写并写入 sw.WriteLine(sr.ReadLine().ToUpper()); //关闭 sw.Flush(); sw.Close(); sr.Close(); ns.Close(); socket.Close(); Console.WriteLine("OK"); } } }}
2008.06.14 / 标签:
C#网络编程 / 分类:
WinForm
o(∩_∩)o…当然和QQ是没得比的了。主要是为了学习C#网络编程及winform高级编程。聊天程序截图如下:
C#局域网聊天器源码:
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Net;
- using System.IO;
- using System.Net.Sockets;
- using System.Threading;
-
- namespace Chat
- {
-
-
-
- public class frmMain : System.Windows.Forms.Form
- {
- private System.Windows.Forms.Button btnConnect;
- private System.Windows.Forms.TextBox txtPort1;
- private System.Windows.Forms.TextBox txtPort2;
- private System.Windows.Forms.ListBox lstInfo;
- private System.Windows.Forms.Button btnSend;
- private System.Windows.Forms.TextBox txtMsg;
-
-
-
- private System.ComponentModel.Container components = null;
-
- public frmMain()
- {
-
-
-
- InitializeComponent();
-
-
-
-
- }
-
-
-
-
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
-
- #region Windows 窗体设计器生成的代码
-
-
-
-
- private void InitializeComponent()
- {
- this.btnConnect = new System.Windows.Forms.Button();
- this.txtPort1 = new System.Windows.Forms.TextBox();
- this.txtPort2 = new System.Windows.Forms.TextBox();
- this.lstInfo = new System.Windows.Forms.ListBox();
- this.btnSend = new System.Windows.Forms.Button();
- this.txtMsg = new System.Windows.Forms.TextBox();
- this.SuspendLayout();
-
-
-
- this.btnConnect.Location = new System.Drawing.Point(200, 224);
- this.btnConnect.Name = "btnConnect";
- this.btnConnect.TabIndex = 0;
- this.btnConnect.Text = "连接";
- this.btnConnect.Click += new System.EventHandler(this.button1_Click);
-
-
-
- this.txtPort1.Location = new System.Drawing.Point(16, 224);
- this.txtPort1.Name = "txtPort1";
- this.txtPort1.Size = new System.Drawing.Size(64, 21);
- this.txtPort1.TabIndex = 1;
- this.txtPort1.Text = "";
-
-
-
- this.txtPort2.Location = new System.Drawing.Point(112, 224);
- this.txtPort2.Name = "txtPort2";
- this.txtPort2.Size = new System.Drawing.Size(64, 21);
- this.txtPort2.TabIndex = 2;
- this.txtPort2.Text = "";
-
-
-
- this.lstInfo.ItemHeight = 12;
- this.lstInfo.Location = new System.Drawing.Point(16, 8);
- this.lstInfo.Name = "lstInfo";
- this.lstInfo.Size = new System.Drawing.Size(464, 136);
- this.lstInfo.TabIndex = 3;
-
-
-
- this.btnSend.Location = new System.Drawing.Point(328, 224);
- this.btnSend.Name = "btnSend";
- this.btnSend.TabIndex = 4;
- this.btnSend.Text = "发送";
- this.btnSend.Click += new System.EventHandler(this.button2_Click);
-
-
-
- this.txtMsg.Location = new System.Drawing.Point(16, 152);
- this.txtMsg.Multiline = true;
- this.txtMsg.Name = "txtMsg";
- this.txtMsg.Size = new System.Drawing.Size(464, 56);
- this.txtMsg.TabIndex = 5;
- this.txtMsg.Text = "";
-
-
-
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(488, 266);
- this.Controls.Add(this.txtMsg);
- this.Controls.Add(this.btnSend);
- this.Controls.Add(this.lstInfo);
- this.Controls.Add(this.txtPort2);
- this.Controls.Add(this.txtPort1);
- this.Controls.Add(this.btnConnect);
- this.Name = "frmMain";
- this.Text = "简单聊天";
- this.ResumeLayout(false);
-
- }
- #endregion
-
-
-
-
- [STAThread]
- static void Main()
- {
- Application.Run(new frmMain());
- }
-
-
class="keyword">private void button2_Click(object sender, System.EventArgs e)
- {
-
- TcpClient TClient = new TcpClient();
-
- TClient.Connect(IPAddress.Parse("127.0.0.1"),int.Parse(this.txtPort1.Text));
-
- NetworkStream ns = TClient.GetStream();
-
- StreamWriter sw =new StreamWriter(ns);
-
- sw.WriteLine(this.txtMsg.Text);
-
- sw.Flush();
-
- sw.Close();
- ns.Close();
- this.lstInfo.Items.Add("你说:"+DateTime.Now.ToString());
- this.lstInfo.Items.Add(this.txtMsg.Text);
- this.txtMsg.Text="";
- }
-
-
-
- void Recieve()
- {
-
- TcpListener TListener = new TcpListener(int.Parse(this.txtPort2.Text));
- TListener.Start();
-
- while(true)
- {
-
- Socket socket = TListener.AcceptSocket();
-
- NetworkStream ns = new NetworkStream(socket);
-
- StreamReader sr =new StreamReader(ns);
-
- string line = sr.ReadLine();
-
- this.lstInfo.Items.Add("对方说:"+DateTime.Now.ToString());
- this.lstInfo.Items.Add(line);
- sr.Close();
- ns.Close();
- socket.Close();
- }
- }
-
- private void button1_Click(ob
ject sender, System.EventArgs e)
- {
-
- Thread thread = new Thread(new ThreadStart(Recieve));
- thread.Start();
- }
- }
- }
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net;using System.IO;using System.Net.Sockets;using System.Threading;namespace Chat{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class frmMain : System.Windows.Forms.Form { private System.Windows.Forms.Button btnConnect; private System.Windows.Forms.TextBox txtPort1; private System.Windows.Forms.TextBox txtPort2; private System.Windows.Forms.ListBox lstInfo; private System.Windows.Forms.Button btnSend; private System.Windows.Forms.TextBox txtMsg; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public frmMain() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.btnConnect = new System.Windows.Forms.Button(); this.txtPort1 = new System.Windows.Forms.TextBox(); this.txtPort2 = new System.Windows.Forms.TextBox(); this.lstInfo = new System.Windows.Forms.ListBox(); this.btnSend = new System.Windows.Forms.Button(); this.txtMsg = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // btnConnect // this.btnConnect.Location = new System.Drawing.Point(200, 224); this.btnConnect.Name = "btnConnect"; this.btnConnect.TabIndex = 0; this.btnConnect.Text = "连接"; this.btnConnect.Click += new System.EventHandler(this.button1_Click); // // txtPort1 // this.txtPort1.Location = new System.Drawing.Point(16, 224); this.txtPort1.Name = "txtPort1"; this.txtPort1.Size = new System.Drawing.Size(64, 21); this.txtPort1.TabIndex = 1; this.txtPort1.Text = ""; // // txtPort2 // this.txtPort2.Location = new System.Drawing.Point(112, 224); this.txtPort2.Name = "txtPort2"; this.txtPort2.Size = new System.Drawing.Size(64, 21); this.txtPort2.TabIndex = 2; this.txtPort2.Text = ""; // // lstInfo // this.lstInfo.ItemHeight = 12; this.lstInfo.Location = new System.Drawing.Point(16, 8); this.lstInfo.Name = "lstInfo"; this.lstInfo.Size = new System.Drawing.Size(464, 136); this.lstInfo.TabIndex = 3; // // btnSend // this.btnSend.Location = new System.Drawing.Point(328, 224); this.btnSend.Name = "btnSend"; this.btnSend.TabIndex = 4; this.btnSend.Text = "发送"; this.btnSend.Click += new System.EventHandler(this.button2_Click); // // txtMsg // this.txtMsg.Location = new System.Drawing.Point(16, 152); this.txtMsg.Multiline = true; this.txtMsg.Name = "txtMsg"; this.txtMsg.Size = new System.Drawing.Size(464, 56); this.txtMsg.TabIndex = 5; this.txtMsg.Text = ""; // // frmMain // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(488, 266); this.Controls.Add(this.txtMsg); this.Controls.Add(this.btnSend); this.Controls.Add(this.lstInfo); this.Controls.Add(this.txtPort2); this.Controls.Add(this.txtPort1); this.Controls.Add(this.btnConnect); this.Name = "frmMain"; this.Text = "简单聊天"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new frmMain()); } private void button2_Click(object sender, System.EventArgs e) { //创建tcpclient对象 TcpClient TClient = new TcpClient(); //连接 TClient.Connect(IPAddress.Parse("127.0.0.1"),int.Parse(this.txtPort1.Text)); //获取网络流 NetworkStream ns = TClient.GetStream(); //创建流写入对象 StreamWriter sw =new StreamWriter(ns); //写入信息 sw.WriteLine(this.txtMsg.Text); //清空缓冲 sw.Flush(); //添加入聊天窗口 sw.Close(); ns.Close(); this.lstInfo.Items.Add("你说:"+DateTime.Now.ToString()); this.lstInfo.Items.Add(this.txtMsg.Text); this.txtMsg.Text=""; } /// <summary> /// 数据接收方法 /// </summary> void Recieve() { //创建监听对象并启动 TcpListener TListener = new TcpListener(int.Parse(this.txtPort2.Text)); TListener.Start(); while(true) { //创建socket Socket socket = TListener.AcceptSocket(); //创建网络流对象 NetworkStream ns = new NetworkStream(socket); //创建流读取对象 StreamReader sr =new StreamReader(ns); //获取信息 string line = sr.ReadLine(); //写入聊天窗口 this.lstInfo.Items.Add("对方说:"+DateTime.Now.ToString()); this.lstInfo.Items.Add(line); sr.Close(); ns.Close(); socket.Close(); } } private void button1_Click(object sender, System.EventArgs e) { //点击此按钮时开始监听 Thread thread = new Thread(new ThreadStart(Recieve)); thread.Start(); } }}
2008.06.12 / 标签: / 分类:
WinForm
TCP类包含连接两个点并在这两个点之间发送数据的方法,一个点由IP地址和端口号组成。现有协议具有定义好的端口号,例如:HTTP使用端口号80,SMTP或电子邮件使用端口号25,而FTP使用端口号21.Internet编号分配管理机构(IANA)负责为这些有名的服务分配端口号。
阅读全文>>
2008.06.12 / 标签: / 分类:
WinForm
System.Net.Sockets包含执行底层操作的类,它处理用于让计算机之间高效通信的代码。此类为需要严密控制网络访问的开发人员提供了Windows.Sockets(Winsock)接口的托管实现。
System.Net.Sockets命名空间包含允许直接发送TCP网络请求或侦听特定端口上的TCP网络请求的相关类。
System.Net.Sockets命名空间的主要类:
阅读全文>>
2008.06.11 / 标签:
web客户端类 / 分类:
WinForm
要向URI标识的资源发送数据和从URI标识的资源接收数据,需要在.NET框架中使用System.Net.WebClient类,此类不能继承,它具有一些方法可用于从URI标识的任何本地Intranet或Internet资源发送和接收数据。.NET框架支持以http:、https:和file:为标识符的URI。
阅读全文>>
2008.06.11 / 标签:
C#网络编程 / 分类:
WinForm
C#网页源码读取器,以前在网上见别人写过这个东西,用来查看某个网站中网页的源代码(若是动态网站,当看到的不是真正的源码)看看里面有没有被挂马,本来还觉得那东西挺神秘的,今天自己写了一下,原来如此easy o(∩_∩)o… 程序源码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.IO;
-
- namespace PageCode
- {
- public partial class frmMain : Form
- {
- public frmMain()
- {
- InitializeComponent();
- }
-
- private void btnGet_Click(object sender, EventArgs e)
- {
-
- WebClient client = new WebClient();
-
- Stream strm = client.OpenRead(txtUrl.Text);
-
- StreamReader sr = new StreamReader(strm);
-
- StringBuilder sb = new StringBuilder();
- string line;
-
- while ((line = sr.ReadLine()) != null)
- {
- sb.Append(line);
- }
- sr.Close();
- strm.Close();
-
- this.txtCode.Text = sb.ToString();
- }
- }
- }
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.IO;namespace PageCode{ public partial class frmMain : Form { public frmMain() { InitializeComponent(); } private void btnGet_Click(object sender, EventArgs e) { //创建WebClient对象 WebClient client = new WebClient(); //创建Stream流对象 Stream strm = client.OpenRead(txtUrl.Text); //创建StreamReader对象 StreamReader sr = new StreamReader(strm); //创建StringBuilder对象用于存储网页代码 StringBuilder sb = new StringBuilder(); string line; //获取网页代码并存入sb中 while ((line = sr.ReadLine()) != null) { sb.Append(line); } sr.Close(); strm.Close(); //显示读取到的代码 this.txtCode.Text = sb.ToString(); } }}
2008.06.11 / 标签:
C#网络编程 / 分类:
WinForm
.NET框架支持在应用程序中实现Internet服务,应用程序可以通过构建可插接式协议来充分利用新的Internet协议。Internet应用程序分为两种类型:客户端应用程序和服务器应用程序。客户端/服务器应用程序最常见的实例是万维网,可以使用浏览器在世界任何地方访问存储在web服务器上的数据。
阅读全文>>
2008.06.05 / 标签:
C#端口扫描器 / 分类:
WinForm
C#端口扫描器,刚做好的时候扫本机测试,结果竟然什么都没有,后来发现原来自己把很多地端口都关闭了,汗~~还以为自己写错代码了。然后用LCX去listen了1-4的端口,打开程序接着扫,嘎嘎~~ 代码是老师讲的,虽然可以扫出来端口,不过我感觉好像代码有问题,不知道是我自己写错了,还是老师讲错了。
源码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
-
- namespace PortScanner
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void btnScan_Click(object sender, EventArgs e)
- {
- string ip = txtIp.Text;
- int StartIp = int.Parse(txtStart.Text);
- int EndIp = int.Parse(txtEnd.Text);
- lstInfo.Items.Clear();
-
-
-
-
- for (int i = StartIp; i <=EndIp; i++)
- {
- try
- {
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- socket.Connect(new IPEndPoint(IPAddress.Parse(ip), i));
- socket.Close();
- lstInfo.Items.Add(ip+":"+i+"开放");
-
- }
- catch { }
- }
-
-
- }
- }
- }
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;namespace PortScanner{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnScan_Click(object sender, EventArgs e) { string ip = txtIp.Text;//获取文本框IP int StartIp = int.Parse(txtStart.Text);//获取起始IP int EndIp = int.Parse(txtEnd.Text);//获取结束IP lstInfo.Items.Clear();//清空信息框 for (int i = StartIp; i <=EndIp; i++) { try { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(new IPEndPoint(IPAddress.Parse(ip), i)); socket.Close(); lstInfo.Items.Add(ip+":"+i+"开放"); } catch { } } } }}
2008.06.05 / 标签: / 分类:
WinForm
题目要求:使用C#编写多线程程序,实现输入两个数字a,b,同时启动4个线程,分别计算加减乘除。并将结果显示出来 提示:创建线程步骤: 1,using System.Threading; 2,Thread th1 = new Thread(new ThreadStrart(方法名)); 3,th1.Start(); 有可能出现的问题:创建线程在static 方法中,所以线程调用的方法也应该是static的。
发现个问题:调试运行时会出错,联机帮助显示:当您在 Visual Studio 调试器中运行代码时,如果您从一个线程访问某个 UI 元素,而该线程不是创建该 UI 元素时所在的线程,则会引发 InvalidOperationException。调试器引发该异常以警告您存在危险的编程操作。UI 元素不是线程安全的,所以只应在创建它们的线程上进行访问。有关更多信息,请参见多线程处理 (Visual Basic)。 如果由于无效参数造成方法调用失败,则应该转为引发 ArgumentException 或其派生类(ArgumentNullException 或 ArgumentOutOfRangeException 异常)之一。而直接运行则没问题。源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Calc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int a;
int b;
private void btnStart_Click(object sender, EventArgs e)
{
try
{
a = int.Parse(txtA.Text);
b = int.Parse(txtB.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
Thread[] thread = new Thread[4];
thread[0] = new Thread(new ThreadStart(Plus));
thread[1] = new Thread(new ThreadStart(Minus));
thread[2] = new Thread(new ThreadStart(Multiply));
thread[3] = new Thread(new ThreadStart(Divide));
thread[0].Start();
thread[1].Start();
thread[2].Start();
thread[3].Start();
}
void Plus()
{
int c = a + b;
label5.Text = c.ToString();
}
void Minus()
{
int c = a - b;
label6.Text = c.ToString();
}
void Multiply()
{
int c = a * b;
label7.Text = c.ToString();
}
void Divide()
{
int c = a / b;
label8.Text = c.ToString();
}
}
}
2008.06.02 / 标签: / 分类:
WinForm
C#音乐媒体文件播放器,估计BUG一大堆,仅用于自己学习。
源码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
-
-
- namespace rectangle
- {
- public partial class MusicPlayer : Form
- {
- public MusicPlayer()
- {
- InitializeComponent();
- }
-
- private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- openFileDialog1.Filter = "mp3文件(*.mp3)|*.mp3|wma文件(*.wma)|*.wma|wav文件(*.wav)|*.wav";
- openFileDialog1.ShowDialog();
-
- if (!this.openFileDialog1.FileName.Equals("openFileDialog1"))
- {
- axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
-
- System.IO.FileInfo file = new System.IO.FileInfo(openFileDialog1.FileName);
- lstMusic.Items.Add(file.Name);
- }
- }
-
- private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- this.axWindowsMediaPlayer1.Ctlcontrols.pause();
- }
-
- private void 播放ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- this.axWindowsMediaPlayer1.Ctlcontrols.play();
- }
- string path;
- private void 打开文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- this.folderBrowserDialog1.ShowDialog();
-
- path = this.folderBrowserDialog1.SelectedPath;
-
- if (!path.Equals(""))
- {
- string[] files;
- files = S
ystem.IO.Directory.GetFiles(path);
- foreach (string str in files)
- {
- System.IO.FileInfo file = new System.IO.FileInfo(str);
- lstMusic.Items.Add(file.Name);
- }
- }
- }
- private void lstMusic_SelectedIndexChanged(object sender, EventArgs e)
- {
-
- this.axWindowsMediaPlayer1.URL = path + "\\"+lstMusic.SelectedItem;
- }
-
- private void trackBar1_Scroll(object sender, EventArgs e)
- {
-
- this.Opacity =(double) (100-this.trackBar1.Value)/100;
- }
-
- private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- Application.Exit();
- }
- }
- }
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace rectangle{ public partial class MusicPlayer : Form { public MusicPlayer() { InitializeComponent(); } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { //文件格式过滤器 openFileDialog1.Filter = "mp3文件(*.mp3)|*.mp3|wma文件(*.wma)|*.wma|wav文件(*.wav)|*.wav"; openFileDialog1.ShowDialog(); //若有选中文件则载入并播放 if (!this.openFileDialog1.FileName.Equals("openFileDialog1")) { axWindowsMediaPlayer1.URL = openFileDialog1.FileName; //过滤文件路径 System.IO.FileInfo file = new System.IO.FileInfo(openFileDialog1.FileName); lstMusic.Items.Add(file.Name); } } private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e) { //播放暂停 this.axWindowsMediaPlayer1.Ctlcontrols.pause(); } private void 播放ToolStripMenuItem_Click(object sender, EventArgs e) { //播放 this.axWindowsMediaPlayer1.Ctlcontrols.play(); } string path;//用于存放文件夹路径 private void 打开文件夹ToolStripMenuItem_Click(object sender, EventArgs e) { this.folderBrowserDialog1.ShowDialog(); //获取文件路径 path = this.folderBrowserDialog1.SelectedPath; //若路径不为空则载入该目录下所有文件 if (!path.Equals("")) { string[] files;//用于存储文件名 files = System.IO.Directory.GetFiles(path);//获取该目录下所有文件名 foreach (string str in files)//载入到歌曲栏 { System.IO.FileInfo file = new System.IO.FileInfo(str); lstMusic.Items.Add(file.Name); } } } private void lstMusic_SelectedIndexChanged(object sender, EventArgs e) { //播放当前选中的歌曲 this.axWindowsMediaPlayer1.URL = path + "\\"+lstMusic.SelectedItem; } private void trackBar1_Scroll(object sender, EventArgs e) { //控制窗体透明度 this.Opacity =(double) (100-this.trackBar1.Value)/100; } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { //退出程序 Application.Exit(); } }}