C#C/S结构网络编程

2008.06.14 / 标签: / 分类: WinForm
Sofa

这个程序所要实现的功能是:客户端发送一段字符串给服务端,服务端将其字母换成大写形式并返回给客户端。当然这个程序还是为了学习而写的,练练手而已。 客户端源码:

 
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8. using System.Net.Sockets;
  9. using System.Net;
  10.  
  11. namespace ClientTest
  12. {
  13.     /// <summary>
  14.     /// Form1 的摘要说明。
  15.     /// </summary>
  16.     public class Form1 : System.Windows.Forms.Form
  17.     {
  18.         private System.Windows.Forms.Button button1;
  19.         private System.Windows.Forms.TextBox textBox1;
  20.         private System.Windows.Forms.TextBox textBox2;
  21.         /// <summary>
  22.         /// 必需的设计器变量。
  23.         /// </summary>
  24.         private System.ComponentModel.Container components = null;
  25.  
  26.         public Form1()
  27.         {
  28.             //
  29.             // Windows 窗体设计器支持所必需的
  30.             //
  31.             InitializeComponent();
  32.  
  33.             //
  34.             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  35.             //
  36.         }
  37.  
  38.         /// <summary>
  39.         /// 清理所有正在使用的资源。
  40.         /// </summary>
  41.         protected override void Dispose( bool disposing )
  42.         {
  43.             if( disposing )
  44.             {
  45.                 if (components != null
  46.                 {
  47.                     components.Dispose();
  48.                 }
  49.             }
  50.             base.Dispose( disposing );
  51.         }
  52.  
  53.         #region Windows 窗体设计器生成的代码
  54.         /// <summary>
  55.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  56.         /// 此方法的内容。
  57.         /// </summary>
  58.         private void InitializeComponent()
  59.         {
  60.             this.button1 = new System.Windows.Forms.Butt
    on();
  61.             this.textBox1 = new System.Windows.Forms.TextBox();
  62.             this.textBox2 = new System.Windows.Forms.TextBox();
  63.             this.SuspendLayout();
  64.             // 
  65.             // button1
  66.             // 
  67.             this.button1.Location = new System.Drawing.Point(56, 80);
  68.             this.button1.Name = "button1";
  69.             this.button1.TabIndex = 0;
  70.             this.button1.Text = "发送";
  71.             this.button1.Click += new System.EventHandler(this.button1_Click);
  72.             // 
  73.             // textBox1
  74.             // 
  75.             this.textBox1.Location = new System.Drawing.Point(48, 8);
  76.             this.textBox1.Name = "textBox1";
  77.             this.textBox1.TabIndex = 1;
  78.             this.textBox1.Text = "";
  79.             // 
  80.             // textBox2
  81.             // 
  82.             this.textBox2.Location = new System.Drawing.Point(48, 40);
  83.             this.textBox2.Name = "textBox2";
  84.             this.textBox2.TabIndex = 2;
  85.             this.textBox2.Text = "";
  86.             // 
  87.             // Form1
  88.             // 
  89.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  90.             this.ClientSize = new System.Drawing.Size(200, 118);
  91.             this.Controls.Add(this.textBox2);
  92.             this.Controls.Add(this.textBox1);
  93.             this.Controls.Add(this.button1);
  94.             this.Name = "Form1";
  95.             this.Text = "Form1";
  96.             this.ResumeLayout(false);
  97.  
  98.         }
  99.         #endregion
  100.  
  101.         /// <summary>
  102.         /// 应用程序的主入口点。
  103.         /// </summary>
  104.         [STAThread]
  105.          lass="keyword">static void Main() 
  106.         {
  107.             Application.Run(new Form1());
  108.         }
  109.  
  110.         private void button1_Click(object sender, System.EventArgs e)
  111.         {
  112.             //创建tcpclient对象
  113.             TcpClient TClient = new TcpClient();
  114.             //创建与服务器的连接
  115.             TClient.Connect(IPAddress.Parse("127.0.0.1"),8000);
  116.             //获取网络流
  117.             NetworkStream ns = TClient.GetStream();
  118.             //创建流写入对象
  119.             StreamWriter sw = new StreamWriter(ns);
  120.             //写入数据
  121.             sw.WriteLine(this.textBox1.Text);
  122.             //清空缓冲
  123.             sw.Flush();
  124.             //创建流读取对象
  125.             StreamReader sr = new StreamReader(ns);
  126.             //读出数据
  127.             this.textBox2.Text=sr.ReadLine();
  128.             
  129.             //关闭
  130.             sr.Close();
  131.             sw.Close();
  132.             ns.Close();
  133.             TClient.Close();
  134.         }
  135.     }
  136. }
  137.  
  138. [/CODE]
  139. Server端源码:
  140. [CODE=csharp]
  141. using System;
  142. using System.IO;
  143. using System.Threading;
  144. using System.Net;
  145. using System.Net.Sockets;
  146.  
  147. namespace ServerTest
  148. {
  149.     /// <summary>
  150.     /// Class1 的摘要说明。
  151.     /// </summary>
  152.     class Class1
  153.     {
  154.         /// <summary>
  155.         /// 应用程序的主入口点。
  156.         /// </summary>
  157.         [STAThread]
  158.         static void Main(string[] args)
  159.         {
  160.             //
  161.             // TODO: 在此处添加代码以启动应用程序
  162.             //
  163.             Console.WriteLine("Running…..");
  164.             Thread thread = new Thread(new ThreadStart(Convert));
  165.             thread.Start();
  166.         }
  167.         /// <summary>
  168.         /// 静态的方法对字符转换
  169.         /// </summary>
  170.         static an> void Convert()
  171.         {
  172.             //创建监听对象并启动
  173.             TcpListener TListener = new TcpListener(8000);
  174.             TListener.Start();
  175.             
  176.             while(true)
  177.             {
  178.                 //获取socket
  179.                 Socket socket = TListener.AcceptSocket();
  180.                 //获取网络流
  181.                 NetworkStream ns = new NetworkStream(socket);
  182.                 //创建流读取对象
  183.                 StreamReader sr = new StreamReader(ns);
  184.                 //创建流写入对象
  185.                 StreamWriter sw = new StreamWriter(ns);
  186.                 //读取数据转换大小写并写入
  187.                 sw.WriteLine(sr.ReadLine().ToUpper());
  188.                 //关闭
  189.                 sw.Flush();
  190.  
  191.                 sw.Close();
  192.                 sr.Close();
  193.                 ns.Close();
  194.                 socket.Close();
  195.                 Console.WriteLine("OK");
  196.             }
  197.         }
  198.     }
  199. }

 

C#局域网聊天

2008.06.14 / 标签: / 分类: WinForm
Sofa

o(∩_∩)o…当然和QQ是没得比的了。主要是为了学习C#网络编程及winform高级编程。聊天程序截图如下: C#局域网聊天器源码:

 
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Net;
  8. using System.IO;
  9. using System.Net.Sockets;
  10. using System.Threading;
  11.  
  12. namespace Chat
  13. {
  14.     /// <summary>
  15.     /// Form1 的摘要说明。
  16.     /// </summary>
  17.     public class frmMain : System.Windows.Forms.Form
  18.     {
  19.         private System.Windows.Forms.Button btnConnect;
  20.         private System.Windows.Forms.TextBox txtPort1;
  21.         private System.Windows.Forms.TextBox txtPort2;
  22.         private System.Windows.Forms.ListBox lstInfo;
  23.         private System.Windows.Forms.Button btnSend;
  24.         private System.Windows.Forms.TextBox txtMsg;
  25.         /// <summary>
  26.         /// 必需的设计器变量。
  27.         /// </summary>
  28.         private System.ComponentModel.Container components = null;
  29.  
  30.         public frmMain()
  31.         {
  32.             //
  33.             // Windows 窗体设计器支持所必需的
  34.             //
  35.             InitializeComponent();
  36.  
  37.             //
  38.             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  39.             //
  40.         }
  41.  
  42.         /// <summary>
  43.         /// 清理所有正在使用的资源。
  44.         /// </summary>
  45.         protected override void Dispose( bool disposing )
  46.         {
  47.             if( disposing )
  48.             {
  49.                 if (components != null
  50.                 {
  51.                     components.Dispose();
  52.                 }
  53.             }
  54.             base.Dispose( disposing );
  55.         }
  56.  
  57.         #region Windows 窗体设计器生成的代码
  58.         /// <summary>
  59.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  60.         /// 此方法的内容。
  61.         
    /// </summary>
  62.         private void InitializeComponent()
  63.         {
  64.             this.btnConnect = new System.Windows.Forms.Button();
  65.             this.txtPort1 = new System.Windows.Forms.TextBox();
  66.             this.txtPort2 = new System.Windows.Forms.TextBox();
  67.             this.lstInfo = new System.Windows.Forms.ListBox();
  68.             this.btnSend = new System.Windows.Forms.Button();
  69.             this.txtMsg = new System.Windows.Forms.TextBox();
  70.             this.SuspendLayout();
  71.             // 
  72.             // btnConnect
  73.             // 
  74.             this.btnConnect.Location = new System.Drawing.Point(200, 224);
  75.             this.btnConnect.Name = "btnConnect";
  76.             this.btnConnect.TabIndex = 0;
  77.             this.btnConnect.Text = "连接";
  78.             this.btnConnect.Click += new System.EventHandler(this.button1_Click);
  79.             // 
  80.             // txtPort1
  81.             // 
  82.             this.txtPort1.Location = new System.Drawing.Point(16, 224);
  83.             this.txtPort1.Name = "txtPort1";
  84.             this.txtPort1.Size = new System.Drawing.Size(64, 21);
  85.             this.txtPort1.TabIndex = 1;
  86.             this.txtPort1.Text = "";
  87.             // 
  88.             // txtPort2
  89.             // 
  90.             this.txtPort2.Location = new System.Drawing.Point(112, 224);
  91.             this.txtPort2.Name = "txtPort2";
  92.             this.txtPort2.Size = new System.Drawing.Size(64, 21);
  93.             this.txtPort2.TabIndex = 2;
  94.             this.txtPort2.Text = "";
  95.             // 
  96.             // lstInfo
  97.             // 
  98.             this.lstInfo.ItemHeight = 12;
  99.             this.lstInfo.Location = new System.Drawing.Point(16, 8);
  100.             this.lstInfo.Name = "lstInfo";
  101.             this.lstInfo.Size = new System.Drawing.Size(464, 136);
  102.             this.lstInfo.TabIndex = 3;
  103.             // 
  104.             // btnSend
  105.             // 
  106.             this.btnSend.Location = new System.Drawing.Point(328, 224);
  107.             this.btnSend.Name = "btnSend";
  108.             this.btnSend.TabIndex = 4;
  109.             this.btnSend.Text = "发送";
  110.             this.btnSend.Click += new System.EventHandler(this.button2_Click);
  111.             // 
  112.             // txtMsg
  113.             // 
  114.             this.txtMsg.Location = new System.Drawing.Point(16, 152);
  115.             this.txtMsg.Multiline = true;
  116.             this.txtMsg.Name = "txtMsg";
  117.             this.txtMsg.Size = new System.Drawing.Size(464, 56);
  118.             this.txtMsg.TabIndex = 5;
  119.             this.txtMsg.Text = "";
  120.             // 
  121.             // frmMain
  122.             // 
  123.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  124.             this.ClientSize = new System.Drawing.Size(488, 266);
  125.             this.Controls.Add(this.txtMsg);
  126.             this.Controls.Add(this.btnSend);
  127.             this.Controls.Add(this.lstInfo);
  128.             this.Controls.Add(this.txtPort2);
  129.             this.Controls.Add(this.txtPort1);
  130.             this.Controls.Add(this.btnConnect);
  131.             this.Name = "frmMain";
  132.             this.Text = "简单聊天";
  133.             this.ResumeLayout(false);
  134.  
  135.         }
  136.         #endregion
  137.  
  138.         /// <summary>
  139.         /// 应用程序的主入口点。
  140.         /// </summary>
  141.         [STAThread]
  142.         static void Main() 
  143.         {
  144.             Application.Run(new frmMain());
  145.         }
  146.  
  147.          class="keyword">private void button2_Click(object sender, System.EventArgs e)
  148.         {
  149.             //创建tcpclient对象
  150.             TcpClient TClient = new TcpClient();
  151.             //连接
  152.             TClient.Connect(IPAddress.Parse("127.0.0.1"),int.Parse(this.txtPort1.Text));
  153.             //获取网络流
  154.             NetworkStream ns = TClient.GetStream();
  155.             //创建流写入对象
  156.             StreamWriter sw =new StreamWriter(ns);
  157.             //写入信息
  158.             sw.WriteLine(this.txtMsg.Text);
  159.             //清空缓冲
  160.             sw.Flush();
  161.             //添加入聊天窗口
  162.             sw.Close();
  163.             ns.Close();
  164.             this.lstInfo.Items.Add("你说:"+DateTime.Now.ToString());
  165.             this.lstInfo.Items.Add(this.txtMsg.Text);
  166.             this.txtMsg.Text="";
  167.         }
  168.         /// <summary>
  169.         /// 数据接收方法
  170.         /// </summary>
  171.         void Recieve()
  172.         {
  173.             //创建监听对象并启动
  174.             TcpListener TListener = new TcpListener(int.Parse(this.txtPort2.Text));
  175.             TListener.Start();
  176.  
  177.             while(true)
  178.             {
  179.                 //创建socket
  180.                 Socket socket = TListener.AcceptSocket();
  181.                 //创建网络流对象
  182.                 NetworkStream ns = new NetworkStream(socket);
  183.                 //创建流读取对象
  184.                 StreamReader sr =new StreamReader(ns);
  185.                 //获取信息
  186.                 string line = sr.ReadLine();
  187.                 //写入聊天窗口
  188.                 this.lstInfo.Items.Add("对方说:"+DateTime.Now.ToString());
  189.                 this.lstInfo.Items.Add(line);
  190.                 sr.Close();
  191.                 ns.Close();
  192.                 socket.Close();
  193.             }
  194.         }
  195.  
  196.         private void button1_Click(ob
    ject
     sender, System.EventArgs e)
  197.         {
  198.             //点击此按钮时开始监听
  199.             Thread thread = new Thread(new ThreadStart(Recieve));
  200.             thread.Start();
  201.         }
  202.     }
  203. }

 

使用TCP类

2008.06.12 / 标签: / 分类: WinForm
Sofa

TCP类包含连接两个点并在这两个点之间发送数据的方法,一个点由IP地址和端口号组成。现有协议具有定义好的端口号,例如:HTTP使用端口号80,SMTP或电子邮件使用端口号25,而FTP使用端口号21.Internet编号分配管理机构(IANA)负责为这些有名的服务分配端口号。
阅读全文>>

套接字编程

2008.06.12 / 标签: / 分类: WinForm
Sofa

System.Net.Sockets包含执行底层操作的类,它处理用于让计算机之间高效通信的代码。此类为需要严密控制网络访问的开发人员提供了Windows.Sockets(Winsock)接口的托管实现。
System.Net.Sockets命名空间包含允许直接发送TCP网络请求或侦听特定端口上的TCP网络请求的相关类。
System.Net.Sockets命名空间的主要类:
阅读全文>>

WebClient类

2008.06.11 / 标签: / 分类: WinForm
Sofa

要向URI标识的资源发送数据和从URI标识的资源接收数据,需要在.NET框架中使用System.Net.WebClient类,此类不能继承,它具有一些方法可用于从URI标识的任何本地Intranet或Internet资源发送和接收数据。.NET框架支持以http:、https:和file:为标识符的URI。
阅读全文>>

C#网页源码读取器

2008.06.11 / 标签: / 分类: WinForm

C#网页源码读取器,以前在网上见别人写过这个东西,用来查看某个网站中网页的源代码(若是动态网站,当看到的不是真正的源码)看看里面有没有被挂马,本来还觉得那东西挺神秘的,今天自己写了一下,原来如此easy o(∩_∩)o… 程序源码:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.IO;
  10.  
  11. namespace PageCode
  12. {
  13.     public partial class frmMain : Form
  14.     {
  15.         public frmMain()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void btnGet_Click(object sender, EventArgs e)
  21.         {
  22.             //创建WebClient对象
  23.             WebClient client = new WebClient();
  24.             //创建Stream流对象
  25.             Stream strm = client.OpenRead(txtUrl.Text);
  26.             //创建StreamReader对象
  27.             StreamReader sr = new StreamReader(strm);
  28.             //创建StringBuilder对象用于存储网页代码
  29.             StringBuilder sb = new StringBuilder();
  30.             string line;
  31.             //获取网页代码并存入sb中
  32.             while ((line = sr.ReadLine()) != null)
  33.             {
  34.                 sb.Append(line);
  35.             }
  36.             sr.Close();
  37.             strm.Close();
  38.             //显示读取到的代码
  39.             this.txtCode.Text = sb.ToString();
  40.         }
  41.     }
  42. }

 

Winform访问Internet

2008.06.11 / 标签: / 分类: WinForm
Sofa

.NET框架支持在应用程序中实现Internet服务,应用程序可以通过构建可插接式协议来充分利用新的Internet协议。Internet应用程序分为两种类型:客户端应用程序和服务器应用程序。客户端/服务器应用程序最常见的实例是万维网,可以使用浏览器在世界任何地方访问存储在web服务器上的数据。
阅读全文>>

C#端口扫描器

2008.06.05 / 标签: / 分类: WinForm
Sofa

C#端口扫描器,刚做好的时候扫本机测试,结果竟然什么都没有,后来发现原来自己把很多地端口都关闭了,汗~~还以为自己写错代码了。然后用LCX去listen了1-4的端口,打开程序接着扫,嘎嘎~~ 代码是老师讲的,虽然可以扫出来端口,不过我感觉好像代码有问题,不知道是我自己写错了,还是老师讲错了。 源码:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.Net.Sockets;
  10.  
  11. namespace PortScanner
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         
  20.         private void btnScan_Click(object sender, EventArgs e)
  21.         {
  22.             string ip = txtIp.Text;//获取文本框IP
  23.             int StartIp = int.Parse(txtStart.Text);//获取起始IP
  24.             int EndIp = int.Parse(txtEnd.Text);//获取结束IP
  25.             lstInfo.Items.Clear();//清空信息框
  26.             
  27.  
  28.  
  29.  
  30.             for (int i = StartIp; i <=EndIp; i++)
  31.             {
  32.                 try
  33.                 {
  34.                     Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  35.                     socket.Connect(new IPEndPoint(IPAddress.Parse(ip), i));
  36.                     socket.Close();
  37.                     lstInfo.Items.Add(ip+":"+i+"开放");
  38.  
  39.                 }
  40.                 catch { }
  41.             }
  42.             
  43.             
  44.         }
  45.     }
  46. }

 

简单C#多线程计算器

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(); 
        } 
    } 
}

C#音乐媒体文件播放器

2008.06.02 / 标签: / 分类: WinForm

C#音乐媒体文件播放器,估计BUG一大堆,仅用于自己学习。 源码:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9.  
  10. namespace rectangle
  11. {
  12.     public partial class MusicPlayer : Form
  13.     {
  14.         public MusicPlayer()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
  20.         {
  21.             //文件格式过滤器
  22.             openFileDialog1.Filter = "mp3文件(*.mp3)|*.mp3|wma文件(*.wma)|*.wma|wav文件(*.wav)|*.wav";
  23.             openFileDialog1.ShowDialog();
  24.             //若有选中文件则载入并播放
  25.             if (!this.openFileDialog1.FileName.Equals("openFileDialog1"))
  26.             {
  27.                 axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
  28.                 //过滤文件路径
  29.                 System.IO.FileInfo file = new System.IO.FileInfo(openFileDialog1.FileName);
  30.                 lstMusic.Items.Add(file.Name);
  31.             }
  32.         }
  33.  
  34.         private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
  35.         {
  36.             //播放暂停
  37.             this.axWindowsMediaPlayer1.Ctlcontrols.pause();
  38.         }
  39.  
  40.         private void 播放ToolStripMenuItem_Click(object sender, EventArgs e)
  41.         {
  42.             //播放
  43.             this.axWindowsMediaPlayer1.Ctlcontrols.play();
  44.         }
  45.         string path;//用于存放文件夹路径
  46.         private void 打开文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
  47.         {
  48.             
  49.             this.folderBrowserDialog1.ShowDialog();
  50.             //获取文件路径
  51.             path = this.folderBrowserDialog1.SelectedPath;
  52.             //若路径不为空则载入该目录下所有文件
  53.             if (!path.Equals(""))
  54.             {
  55.                 string[] files;//用于存储文件名
  56.                 files = S
    ystem.IO.Directory.GetFiles(path);
    //获取该目录下所有文件名
  57.                 foreach (string str in files)//载入到歌曲栏
  58.                 {
  59.                     System.IO.FileInfo file = new System.IO.FileInfo(str);
  60.                     lstMusic.Items.Add(file.Name);
  61.                 }
  62.             }
  63.         }
  64.         private void lstMusic_SelectedIndexChanged(object sender, EventArgs e)
  65.         {
  66.             //播放当前选中的歌曲
  67.             this.axWindowsMediaPlayer1.URL = path + "\\"+lstMusic.SelectedItem;
  68.         }
  69.  
  70.         private void trackBar1_Scroll(object sender, EventArgs e)
  71.         {
  72.             //控制窗体透明度
  73.             this.Opacity =(double) (100-this.trackBar1.Value)/100;
  74.         }
  75.  
  76.         private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  77.         {
  78.             //退出程序
  79.             Application.Exit();
  80.         }
  81.     }
  82. }

 

无觅相关文章插件,快速提升流量