using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Threading;
using OpenNETCF.Desktop.Communication;
namespace Deployment
{
public class Form1 : System.Windows.Forms.Form
{
private const string ProgramFiles = "Program Files";
private const string StartMenu = "Start Menu";
private const string StartUp = "StartUp";
private const string ConnectedMsg = "Device connected";
private const string DisconnectedMsg = "No device connected";
private const string ConnectingMsg = "Connecting...";
private string MobileExeFileList = "";
private bool RefreshListsNeeded = false;
private string AppPath="";
private ContextMenu tvMobileProgramFilesMenu = new ContextMenu();
private RAPI rapi = new RAPI();
#region Constructor
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public Form1()
{
InitializeComponent();
rapi.ActiveSync.Active += new ActiveHandler(ActiveSync_Active);
rapi.ActiveSync.Disconnect += new DisconnectHandler(ActiveSync_Disconnect);
rapi.ActiveSync.Listen += new ListenHandler(ActiveSync_Listen);
rapi.ActiveSync.Answer += new AnswerHandler(ActiveSync_Answer);
}
#region Form Load
private void Form1_Load(object sender, System.EventArgs e)
{
Hourglass(true);
try
{
this.SetAppPath();
this.CreateAppFolders();
// set a registry key so that all future ActiveSync
// connections launch as Guest Only without that
// annoying prompt. The .ConnectAsGuestOnly property
// will create the following key if it doesn't already
// exist. Simply use regedit to delete this key or
// set the DWORD value = 0
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows CE Services
// GuestOnly DWORD = 1
rapi.ActiveSync.ConnectAsGuestOnly = true;
this.lblStatus.Text = DisconnectedMsg;
this.Connect();
if (rapi.Connected)
{
this.lblStatus.Text = ConnectedMsg;
}
tvMobileProgramFilesMenu.MenuItems.Add("Delete",
new EventHandler(tvMobileProgramFilesRightClickDelete));
Application.DoEvents();
this.LoadLocalFileLists();
this.LoadLocalProgramFiles();
RefreshListsNeeded = true;
}
catch (Exception err) { Hourglass(false); ShowError(err.Message); }
finally { Hourglass(false); }
}
#endregion
#region Form Closed
private void Form1_Closed(object sender, System.EventArgs e)
{
try
{
rapi.Disconnect();
rapi.Dispose();
}
catch { }
}
#endregion
#region Exit
private void cmdExit_Click(object sender, System.EventArgs e)
{
this.Close();
Application.Exit();
}
#endregion
#region Create App Folders
private void CreateAppFolders()
{
if (!Directory.Exists(Path.Combine(AppPath,ProgramFiles)))
{
Directory.CreateDirectory(Path.Combine(AppPath,ProgramFiles));
}
if (!Directory.Exists(Path.Combine(AppPath,StartUp)))
{
Directory.CreateDirectory(Path.Combine(AppPath,StartUp));
}
if (!Directory.Exists(Path.Combine(AppPath,StartMenu)))
{
Directory.CreateDirectory(Path.Combine(AppPath,StartMenu));
}
}
#endregion
#region Set App Path
private void SetAppPath()
{
try
{
AppPath = Path.GetFullPath(".");
AppPath = AppPath.Replace(@"\bin\Debug","");
AppPath = AppPath.Replace(@"\bin\Release","");
if (AppPath.EndsWith(@"\") == false) { AppPath += @"\"; }
}
catch (Exception) { throw; }
}
#endregion
#region Show Error
private void ShowError(string msg)
{
MessageBox.Show(msg);
}
#endregion
#region Timer
private void timer1_Tick(object sender, System.EventArgs e)
{
if (!RefreshListsNeeded) { return; }
RefreshListsNeeded = false;
this.LoadMobileProgramFiles();
this.LoadMobileStartMenuList();
this.LoadMobileStartUpList();
return;
}
#endregion
#region Load Local File Lists
private void LoadLocalFileLists()
{
DirectoryInfo d;
FileInfo[] fil;
Hourglass(true);
try
{
d = new DirectoryInfo(Path.Combine(AppPath,StartMenu));
this.lstStartMenuShortCuts.Items.Clear();
this.lstStartUpShortCuts.Items.Clear();
fil = d.GetFiles();
foreach(FileInfo f in fil)
{
this.lstStartMenuShortCuts.Items.Add(new MyApp.CustomItem(0,f.Name,f.FullName,false));
}
d = null;
d = new DirectoryInfo(Path.Combine(AppPath,StartUp));
fil = d.GetFiles();
foreach(FileInfo f in fil)
{
this.lstStartUpShortCuts.Items.Add(new MyApp.CustomItem(0,f.Name,f.FullName,false));
}
d = null;
}
catch (Exception e) { ShowError(e.Message); }
finally
{
this.Invalidate();
this.Disconnect();
Hourglass(false);
}
}
#endregion
#region Load Local Program Files
private void LoadLocalProgramFiles()
{
TreeNode node;
Hourglass(true);
try
{
tvProgramFiles.Invalidate();
DirectoryInfo d = new DirectoryInfo(Path.Combine(AppPath,ProgramFiles));
this.tvProgramFiles.Nodes.Clear();
this.tvProgramFiles.BeginUpdate();
node = new TreeNode(ProgramFiles,0,0);
node.Tag = new MyApp.CustomItem(0,d.Name,ProgramFiles,true);
this.tvProgramFiles.Nodes.Add(node);
LoadFileList(d,node,false);
CollapseProgramFolders(node);
tvProgramFiles.Refresh();
node.Expand();
node.EnsureVisible();
}
catch (Exception) { throw; }
finally
{
this.tvProgramFiles.EndUpdate();
Hourglass(false);
}
}
#endregion
#region Load Mobile Start Menu List
private void LoadMobileStartMenuList()
{
OpenNETCF.Desktop.Communication.FileList fl;
OpenNETCF.Desktop.Communication.FileInformation fi;
Hourglass(true);
try
{
this.lstMobileStartMenuShortCuts.Items.Clear();
if (!Connect()) { return; }
fl = rapi.EnumFiles(@"\Windows\" + StartMenu + @"\*.lnk");
if (fl != null)
{
for(int i=0;i 0)
{
this.CheckAllChildNodes(node, nodeChecked);
}
if (!node.Checked)
{
node.Collapse();
}
else
{
node.ExpandAll();
}
}
}
#endregion
#region tvMobileProgramFiles Right Click Delete
private void tvMobileProgramFilesRightClickDelete(object sender, System.EventArgs e)
{
Hourglass(true);
try
{
this.Connect();
MyApp.CustomItem item = (MyApp.CustomItem)tvMobileProgramFiles.SelectedNode.Tag;
DeleteDeviceFile(ConvertPath(item.Tag));
this.LoadMobileProgramFiles();
}
catch (Exception err) { ShowError(err.Message); }
finally { this.Disconnect(); Hourglass(false); }
}
#endregion
#region tvMobileProgramFiles MouseUp
private void tvMobileProgramFiles_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if ( e.Button != MouseButtons.Right ) return;
Point pt = new Point( e.X, e.Y );
tvMobileProgramFiles.PointToClient(pt);
TreeNode Node = tvMobileProgramFiles.GetNodeAt(pt);
if ( Node == null ) return;
if (!Node.Bounds.Contains(pt)) { return; }
tvMobileProgramFiles.SelectedNode = Node;
tvMobileProgramFilesMenu.Show(tvMobileProgramFiles,pt);
}
#endregion
#region tvMobileProgramFiles KeyUp
private void tvMobileProgramFiles_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode != Keys.Apps) { return; }
Point pt = new Point(tvMobileProgramFiles.SelectedNode.Bounds.Left,
tvMobileProgramFiles.SelectedNode.Bounds.Bottom );
tvMobileProgramFilesMenu.Show(tvMobileProgramFiles,pt);
}
#endregion
}
}
|