在PDA安装包中使用RAPI动态复制文件到设备

因为软件需要,在PDA安装程序的过程中需要复制特定目录下的文件到设备中去,一般情况下我们可以将这些文件嵌入到CAB包中,但在此程序中,该目录结构复杂,数据文件数量非常多,而且客户在向最终用户分发程序时可能根据需要增删这些文件,因此必须编码以动态复制文件。

基本的思路是采用ActiveSync提供的RAPI,由于程序采用C#编写,直接调用API难度大且易出错,这里采用了OpenNetCF中的类库,在名为OpenNETCF.Desktop.Communication的命名空间下面,提供了对RAPI的完整封装。

遵循固定的流程,首先创建CAB项目,下面为改CAB中installer类所调用的复制文件代码断:
None.gif public   class  Copyer
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private RAPI rapi = new RAPI();
InBlock.gif        
private string ROOT_DIR_PDA = "\\Program Files\\XXX\\";
InBlock.gif        
private string cdir = "";
InBlock.gif        
public Copyer(string root)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            cdir 
= root;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void InternalCopyDirectory(DirectoryInfo info)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string src = info.FullName;
InBlock.gif                
string dest = info.FullName.Replace(cdir, ROOT_DIR_PDA);
InBlock.gif                
if (rapi.DeviceFileExists(dest) == false)
InBlock.gif                    rapi.CreateDeviceDirectory(dest);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void InternalCopyFile(FileInfo file)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string src2 = file.FullName;
InBlock.gif                
string dest2 = file.FullName.Replace(cdir, ROOT_DIR_PDA);
InBlock.gif                
if (rapi.DeviceFileExists(dest2) == false)
InBlock.gif                    rapi.CopyFileToDevice(src2, dest2);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void InternalCopy(DirectoryInfo info)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InternalCopyDirectory(info);
InBlock.gif            FileInfo[] files 
= info.GetFiles();
InBlock.gif            
foreach (FileInfo f in files)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                InternalCopyFile(f);
InBlock.gif                System.Windows.Forms.Application.DoEvents();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Copy(DirectoryInfo info)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            rapi.Connect(
true20000);
InBlock.gif            
InBlock.gif            InternalCopy(info);
InBlock.gif            DirectoryInfo[] infos 
= info.GetDirectories("*", SearchOption.AllDirectories);
InBlock.gif            
foreach (DirectoryInfo i in infos)
InBlock.gif                InternalCopy(i);
InBlock.gif
InBlock.gif            rapi.Disconnect();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

由于PC和PDA的通信速度比较慢,在复制文件的过程中,需要显示进度条以提高用户体验。对于MSI中如何调用API显示进度条不是很熟悉,因此我在InternalCopy中采用Application.DoEvents激活主线程中的定时器以更新一个包含了进度条的自定义窗体,用以模拟复制进度,有点欺骗用户的感觉,呵呵。

现在,剩下的就是创建MSI工程,添加CAB等一些常规的操作了,当然还有必须的参数传递代码:)此处省略。

转载于:https://www.cnblogs.com/swnuwangyun/archive/2006/12/27/605198.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值