⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 WindowsMobile平台应用开发一书的源码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace Receiver
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IrDADeviceInfo[] irDevices;
            IrDAClient irClient = new IrDAClient();
            string irServiceName = "IrDATest";
            int buffersize = 256;


            // 首先尝试检索红外网络
            irDevices = irClient.DiscoverDevices(2);

            // 如果没有找到红外设备则退出
            if ((irDevices.Length == 0))
            {
                MessageBox.Show("No remote infrared devices found.");
                return;
            }

            // 连接到第一个设备上
            IrDAEndPoint irEndP =
                new IrDAEndPoint(irDevices[0].DeviceID, irServiceName);
            irClient.Connect(irEndP);

            // 建立一个流用于写文件
            Stream writeStream;
            try
            {
                writeStream = new FileStream(".\\My Documents\\receive.txt",
                    FileMode.OpenOrCreate);
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Cannot open file for writing. " + Ex.Message);
                return;
            }

            // 获得IrDAClient的数据流
            Stream baseStream = irClient.GetStream();

            // 建立读取文件的缓冲区.
            byte[] buffer = new byte[buffersize];
            Int64 numToRead;
            Int64 numRead;

            // 首先读取出并计算出文件的长度来.
            numToRead = 8;
            try
            {
                while ((numToRead > 0))
                {
                    numRead = baseStream.Read(buffer, 0,
                        Convert.ToInt32(numToRead));
                    numToRead = (numToRead - numRead);
                }
            }
            catch (Exception exReadIn)
            {
                MessageBox.Show(("Read in: " + exReadIn.Message));
            }
            numToRead = BitConverter.ToInt64(buffer, 0);

            try
            {
                // 然后将流中其他的数据写入文件流
                while ((numToRead > 0))
                {
                    numRead = baseStream.Read(buffer, 0, buffer.Length);
                    numToRead = (numToRead - numRead);
                    writeStream.Write(buffer, 0, Convert.ToInt32(numRead));
                }
                //写完数据后关闭文件流
                writeStream.Close();
                MessageBox.Show("File received.");
            }
            catch (Exception exWriteOut)
            {
                MessageBox.Show(("Write out: " + exWriteOut.Message));
            }
            //关闭IrDAClient对象的数据流和连接
            baseStream.Close();
            irClient.Close();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -