zoukankan      html  css  js  c++  java
  • WIN CE和电脑之间的文件拷贝(2) Form1.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.Runtime.InteropServices;
    using System.IO;

    namespace RpcWince
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public int RapiInit()
            {
                int ret = CeRapiInit();

                if( ret != 0)
                {
                    // 连接失败,获取失败代码
                    int e = CeRapiGetError();

                    // 抛出异常
                    Marshal.ThrowExceptionForHR(ret);
                }
                return ret;
                // 连接成功
                // To Do
            }
            public void RAPIUninit()
            {
                CeRapiUninit();
            }
           
            // 声明要引用的API
            [DllImport("rapi.dll", CharSet=CharSet.Unicode)]
            internal static extern int CeRapiUninit();

            [DllImport("rapi.dll", CharSet=CharSet.Unicode)]
            internal static extern int CeRapiGetError();

            [DllImport("rapi.dll", CharSet=CharSet.Unicode)]
            internal static extern int CeRapiInit();

            // 声明要引用的API
            [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
            internal static extern int CeCloseHandle(IntPtr hObject);

            [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
            internal static extern int CeWriteFile(IntPtr hFile, byte[] lpBuffer,
                                 int nNumberOfbytesToWrite, ref int lpNumberOfbytesWritten, int lpOverlapped);

            [DllImport("rapi.dll",   CharSet   =   CharSet.Unicode,   SetLastError   =   true)]  
            internal   static   extern   int   CeReadFile(IntPtr   hFile,   byte[]   lpBuffer,   int   nNumberOfbytesToRead,   ref   int   lpNumberOfbytesRead,   int   lpOverlapped);  

            //[DllImport("rapi.dll",   CharSet   =   CharSet.Unicode)]  
            //internal   static   extern   int   CeCloseHandle(IntPtr   hObject);    

            //private   const   short   CREATE_NEW   =   1;  
            private   const   short   CREATE_ALWAYS   =   2;  
            //private   const   uint   GENERIC_WRITE   =   0x40000000;  
            private   const   uint   GENERIC_READ   =   0x80000000;  
            private   const   short   OPEN_EXISTING   =   3;  
            //private   const   short   FILE_ATTRIBUTE_NORMAL   =   0x80;  
            internal   const   int   ERROR_NO_MORE_FILES   =   18;  
            //private   const   short   INVALID_HANDLE_VALUE   =   -1;  


            [DllImport("rapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            internal static extern IntPtr CeCreateFile(
                                 string lpFileName,
                                 uint dwDesiredAccess,
                                 int dwShareMode,
                                 int lpSecurityAttributes,
                                 int dwCreationDisposition,
                                 int dwFlagsAndAttributes,
                                 int hTemplateFile);
            [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
            internal static extern int CeCopyFile(string lpExistingFileName, string lpNewFileName, int bFailIfExists);

            [DllImport("rapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            internal static extern int CeGetSystemInfo(out SYSTEM_INFO pSI);

            [DllImport("rapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            internal static extern bool CeGetVersionEx(out OSVERSIONINFO lpVersionInformation);

            private const uint GENERIC_WRITE = 0x40000000;          // 设置读写权限
            private const short CREATE_NEW = 1;                     // 创建新文件
            private const short FILE_ATTRIBUTE_NORMAL = 0x80;       // 设置文件属性
            private const short INVALID_HANDLE_VALUE = -1;          // 错误句柄

            IntPtr remoteFile = IntPtr.Zero;
            String LocalFileName = @"d:\test.txt";                  // 本地计算机文件名
            String RemoteFileName = @"\test.txt";                   // 远程设备文件名
            byte[] buffer = new byte[0x1000];                       // 传输缓冲区定义为4k
            FileStream localFile;

            int bytesread = 0;
            int byteswritten = 0;
            int filepos = 0;

            //从PC拷贝到CE设备
            public void RapiPCtoCEFile()
            {
                 // 创建远程文件
                 remoteFile = CeCreateFile(RemoteFileName, GENERIC_WRITE, 0, 0, CREATE_NEW,
                                                         FILE_ATTRIBUTE_NORMAL, 0);

                 // 检查文件是否创建成功
                 if ((int)remoteFile == INVALID_HANDLE_VALUE)
                 {
                    throw new Exception("Could not create remote file");
                 }

                 // 打开本地文件
                 localFile = new FileStream(LocalFileName, FileMode.Open);

                 // 读取4K字节
                 bytesread = localFile.Read(buffer, filepos, buffer.Length);
                 while(bytesread > 0)
                 {
                     // 移动文件指针到已读取的位置
                     filepos += bytesread;

                     // 写缓冲区数据到远程设备文件
                     if(! Convert.ToBoolean(CeWriteFile(remoteFile, buffer, bytesread,
                                         ref byteswritten, 0)))
                     { // 检查是否成功,不成功关闭文件句柄,抛出异常
                         CeCloseHandle(remoteFile);
                         throw new Exception("Could not write to remote file");
                     }
                     try
                     {
                         // 重新填充本地缓冲区
                         bytesread = localFile.Read(buffer, 0, buffer.Length);
                     }
                     catch(Exception)
                     {
                        bytesread = 0;
                     }
                 }

                 // 关闭本地文件
                 localFile.Close();

                 // 关闭远程文件
                 CeCloseHandle(remoteFile);
            }

            //將遠程設備上的文件複製到PC文件目錄下  
            public   void   CopyFileFromDevice(string   LocalFileName,   string   RemoteFileName,   bool   Overwrite)  
            {  
                  FileStream   localFile;  
                  IntPtr   remoteFile   =   IntPtr.Zero;  
                  int   bytesread   =   0;  
                  int   create   =   Overwrite   ?   CREATE_ALWAYS   :   CREATE_NEW;  
                  byte[]   buffer   =   new   byte[0x1000];   //   4k   transfer   buffer  

                  //   open   the   remote   (device)   file  
                  remoteFile   =   CeCreateFile(RemoteFileName,   GENERIC_READ,   0,   0,   OPEN_EXISTING,   FILE_ATTRIBUTE_NORMAL,   0);  

                  //   check   for   success  
                  if   ((int)remoteFile   ==   INVALID_HANDLE_VALUE)  
                  {  
                          throw   new   Exception("不能打開遠程設備上的Material_Tx.xml文件");  
                  }  

                  //   create   the   local   file  
                  localFile   =   new   FileStream(LocalFileName,   Overwrite   ?   FileMode.Create   :   FileMode.CreateNew,   FileAccess.Write);  

                  //   read   data   from   remote   file   into   buffer  
                  CeReadFile(remoteFile,   buffer,   0x1000,   ref   bytesread,   0);  
                  while   (bytesread   >   0)  
                  {  
                          //   write   it   into   local   file  
                          localFile.Write(buffer,   0,   bytesread);  

                          //   get   more   data  
                          if   (!Convert.ToBoolean(CeReadFile(remoteFile,   buffer,   0x1000,   ref   bytesread,   0)))  
                          {  
                                  CeCloseHandle(remoteFile);  
                                  localFile.Close();  
                                  throw   new   Exception("讀取遠程設備上的Material_Tx.xml文件失敗!");  
                          }  
                  }  

                  //   close   the   remote   file  
                  CeCloseHandle(remoteFile);  

                  localFile.Flush();  

                  //   close   the   local   file  
                  localFile.Close();  
            }  
       

            private void button1_Click(object sender, EventArgs e)
            {
                if (RapiInit() == 0)
                {
                   
                    labelstate.Text = "连接成功";
                    button1.Enabled = false;
                }
            }

            private void button2_Click(object sender, EventArgs e)
            {
                RAPIUninit();
                labelstate.Text = "已断开连接";
                button1.Enabled = true;
            }

            private void button3_Click(object sender, EventArgs e)
            {
                RapiPCtoCEFile();
            }

            //CE拷贝到PC
            private void button6_Click(object sender, EventArgs e)
            {
                CopyFileFromDevice(@"C:\pc.txt", @"\ce.txt", false);//false是覆盖
            }

            private void button4_Click(object sender, EventArgs e)
            {
                //只支持CE设备上的文件操作,而不能用于在PC与CE设备之间传输文件
                int bRet = CeCopyFile(@"\test.txt", @"C:\test1.txt", 0);//0文件存在则覆盖,1不能覆盖
                if (bRet == 1)
                {
                    MessageBox.Show("success");
                }
            }

            private void button5_Click(object sender, EventArgs e)
            {
                SYSTEM_INFO sys;
                CeGetSystemInfo(out sys);
                labelstate.Text = sys.dwProcessorType.ToString();

                OSVERSIONINFO osv;
                CeGetVersionEx(out osv);
                textBox1.Text = osv.dwOSVersionInfoSize.ToString() + "\r\n" +
                    osv.dwMajorVersion.ToString() + "." +
                    osv.dwMinorVersion.ToString() + "." +
                    osv.dwBuildNumber.ToString();
            }
            ///
            /// OSVERSIONINFO platform type 
            ///  
            public enum PlatformType : int 
            {  
                ///  
                /// Win32 on Windows CE.  
                ///   
                VER_PLATFORM_WIN32_CE = 3 
            } 
            /// 
            /// 操作系统版本信息 
            ///  
            public struct OSVERSIONINFO 
            {  
                internal int dwOSVersionInfoSize;  
                ///  
                /// 主版本信息  
                ///   
                public int dwMajorVersion;  
                ///   
                /// 副版本信息  
                ///   
                public int dwMinorVersion;  
                ///   
                /// 编译信息  
                ///   
                public int dwBuildNumber;  
                ///  
                /// 操作系统类型  
                ///   
                public PlatformType dwPlatformId; 
            }


            public struct SYSTEM_INFO
            {
                /// <summary>
                /// Processor architecture
                /// </summary>
                public ProcessorArchitecture wProcessorArchitecture;
                internal ushort wReserved;
                /// <summary>
                /// Specifies the page size and the granularity of page protection and commitment.
                /// </summary>
                public int dwPageSize;
                /// <summary>
                /// Pointer to the lowest memory address accessible to applications and dynamic-link libraries (DLLs).
                /// </summary>
                public int lpMinimumApplicationAddress;
                /// <summary>
                /// Pointer to the highest memory address accessible to applications and DLLs.
                /// </summary>
                public int lpMaximumApplicationAddress;
                /// <summary>
                /// Specifies a mask representing the set of processors configured into the system. Bit 0 is processor 0; bit 31 is processor 31.
                /// </summary>
                public int dwActiveProcessorMask;
                /// <summary>
                /// Specifies the number of processors in the system.
                /// </summary>
                public int dwNumberOfProcessors;
                /// <summary>
                /// Specifies the type of processor in the system.
                /// </summary>
                public ProcessorType dwProcessorType;
                /// <summary>
                /// Specifies the granularity with which virtual memory is allocated.
                /// </summary>
                public int dwAllocationGranularity;
                /// <summary>
                /// Specifies the system抯 architecture-dependent processor level.
                /// </summary>
                public short wProcessorLevel;
                /// <summary>
                /// Specifies an architecture-dependent processor revision.
                /// </summary>
                public short wProcessorRevision;
            }
            public enum ProcessorArchitecture : short
            {
                /// <summary>
                /// Intel
                /// </summary>
                Intel = 0,
                /// <summary>
                /// MIPS
                /// </summary>
                MIPS = 1,
                /// <summary>
                /// Alpha
                /// </summary>
                Alpha = 2,
                /// <summary>
                /// PowerPC
                /// </summary>
                PPC = 3,
                /// <summary>
                /// Hitachi SHx
                /// </summary>
                SHX = 4,
                /// <summary>
                /// ARM
                /// </summary>
                ARM = 5,
                /// <summary>
                /// IA64
                /// </summary>
                IA64 = 6,
                /// <summary>
                /// Alpha 64
                /// </summary>
                Alpha64 = 7,
                /// <summary>
                /// Unknown
                /// </summary>
                Unknown = -1
            }
            public enum ProcessorType : int
            {
                /// <summary>
                /// 386
                /// </summary>
                PROCESSOR_INTEL_386 = 386,
                /// <summary>
                /// 486
                /// </summary>
                PROCESSOR_INTEL_486 = 486,
                /// <summary>
                /// Pentium
                /// </summary>
                PROCESSOR_INTEL_PENTIUM = 586,
                /// <summary>
                /// P2
                /// </summary>
                PROCESSOR_INTEL_PENTIUMII = 686,
                /// <summary>
                /// IA 64
                /// </summary>
                PROCESSOR_INTEL_IA64 = 2200,
                /// <summary>
                /// MIPS 4000 series
                /// </summary>
                PROCESSOR_MIPS_R4000 = 4000,
                /// <summary>
                /// Alpha 21064
                /// </summary>
                PROCESSOR_ALPHA_21064 = 21064,
                /// <summary>
                /// PowerPC 403
                /// </summary>
                PROCESSOR_PPC_403 = 403,
                /// <summary>
                /// PowerPC 601
                /// </summary>
                PROCESSOR_PPC_601 = 601,
                /// <summary>
                /// PowerPC 603
                /// </summary>
                PROCESSOR_PPC_603 = 603,
                /// <summary>
                /// PowerPC 604
                /// </summary>
                PROCESSOR_PPC_604 = 604,
                /// <summary>
                /// PowerPC 620
                /// </summary>
                PROCESSOR_PPC_620 = 620,
                /// <summary>
                /// Hitachi SH3
                /// </summary>
                PROCESSOR_HITACHI_SH3 = 10003,
                /// <summary>
                /// Hitachi SH3E
                /// </summary>
                PROCESSOR_HITACHI_SH3E = 10004,
                /// <summary>
                /// Hitachi SH4
                /// </summary>
                PROCESSOR_HITACHI_SH4 = 10005,
                /// <summary>
                /// Motorola 821
                /// </summary>
                PROCESSOR_MOTOROLA_821 = 821,
                /// <summary>
                /// Hitachi SH3
                /// </summary>
                PROCESSOR_SHx_SH3 = 103,
                /// <summary>
                /// Hitachi SH4
                /// </summary>
                PROCESSOR_SHx_SH4 = 104,
                /// <summary>
                /// Intel StrongARM
                /// </summary>
                PROCESSOR_STRONGARM = 2577,//---98x
                /// <summary>
                /// ARM720
                /// </summary>
                PROCESSOR_ARM720 = 1824,
                /// <summary>
                /// ARM820
                /// </summary>
                PROCESSOR_ARM820 = 2080,
                /// <summary>
                /// ARM920
                /// </summary>
                PROCESSOR_ARM920 = 2336,
                /// <summary>
                /// ARM 7
                /// </summary>
                PROCESSOR_ARM_7TDMI = 70001
            }

        }
    }

  • 相关阅读:
    Python网络爬虫规则之实例
    Syncthing:同步window和linux文件
    阿里云AIoT云端一体:迎接云原生+低代码时代的到来
    低代码时代的物联网快速构建工具-YFIOs
    阿里云、华为云和腾讯云等多家物联网平台的异同
    全志 Fex文件
    le16_to_cpu
    无线充电技术简介
    AK47所向披靡,内存泄漏一网打尽
    zRAM内存压缩技术原理与应用
  • 原文地址:https://www.cnblogs.com/gaolijun1986/p/2131114.html
Copyright © 2011-2022 走看看