@import url("http://www.blogger.com/css/blog_controls.css"); @import url("http://www.blogger.com/dyn-css/authorization.css?blogID=8706105"); Hardik Shah - Trying to be with the Technology
Networks

Thursday, August 18, 2005

File Explorer for Magneto Smartphone

Having used a Windows Mobile 2003 Smartphone for over three months, it was now the time to use a "flashed" Magneto (Windows Mobile 2005)smartphone. Having done all the testing and R&D on the OS, there was one thing which I terribly missed in Magneto. Rather I missed two things.

1. Magneto doesnt support MMS ( Wondering what with I do with the Cam in the phone now)
2. There is no File Explorer (becomes diffcult to browse though the contents of the storage card).

Going back home, I though I should not wait for anything and write an app for FileExplorer. With very little experience in writing code (I am not a developer, I am more into System Administration), and ofcouse with the help of Andy Wigley's book, ".NET Compact Framework Core Reference" (MS Press), which contains lots of valuable Compact Framework information (it doesn't cover the SmartPhone form factor in depth), I started writing the app. While going through lots of articles, I also came across an article by Wei-Meng Lee, which I must say - helped me a lot in writing the app. Here is the complete code which I have written. Do give me your feedback and comments on how to improve my code and the performance of the Explorer.

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace Explorer
{

/// Summary description for Tree.
public class Tree : System.Windows.Forms.Form
{
const int icoOpen = 0;
const int icoClose = 1;
const int icoFile = 2;
bool copy = true; // whether it is a copy operation
string fileName; // filename of file to be copied or cut
string pathName; // full pathname of file to be copied or cut

private TreeView treeView1;
private MenuItem menuItem1;
private MenuItem menuItem2;
private MenuItem menuItem3;
private MenuItem menuItem4;
private MenuItem menuItem5;
private MenuItem mnuPaste;
private ImageList imageList2;

public event EventHandler NotifyParent;

protected void OnNotifyParent()
{
if (NotifyParent != null) NotifyParent(this.pathName , EventArgs.Empty);
}

/// Main menu for the form.
private System.Windows.Forms.MainMenu mainMenu1;

public Tree()
{
InitializeComponent();
}

/// Clean up resources

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}

#region Windows Form Designer generated code


/// Required method for Designer support - do not modify the contents of this method with the code editor.

private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Tree));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.mnuPaste = new System.Windows.Forms.MenuItem();
this.treeView1 = new System.Windows.Forms.TreeView();
this.imageList2 = new System.Windows.Forms.ImageList();

// mainMenu1
this.mainMenu1.MenuItems.Add(this.menuItem1);
this.mainMenu1.MenuItems.Add(this.menuItem2);

// menuItem1
this.menuItem1.Text = "Exit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

// menuItem2
this.menuItem2.MenuItems.Add(this.menuItem3);
this.menuItem2.MenuItems.Add(this.menuItem4);
this.menuItem2.MenuItems.Add(this.menuItem5);
this.menuItem2.MenuItems.Add(this.mnuPaste);
this.menuItem2.Text = "File";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);

// menuItem3
this.menuItem3.Text = "Copy";
this.menuItem3.Click += new System.EventHandler(this.mnuCopy_Click);

// menuItem4
this.menuItem4.Text = "Cut";
this.menuItem4.Click += new System.EventHandler(this.menuCut_Click);

// menuItem5
this.menuItem5.Text = "Delete";
this.menuItem5.Click += new System.EventHandler(this.mnuDelete_Click);

// mnuPaste
this.mnuPaste.Text = "Paste";
this.mnuPaste.Click += new System.EventHandler(this.mnuPaste_Click);


// treeView1
this.treeView1.ImageIndex = 0;
this.treeView1.ImageList = this.imageList2;
this.treeView1.Indent = 20;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.SelectedImageIndex = 0;
this.treeView1.Size = new System.Drawing.Size(176, 177);
this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
this.imageList2.Images.Clear();
this.imageList2.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
this.imageList2.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
this.imageList2.Images.Add(((System.Drawing.Image)(resources.GetObject("resource2"))));


// Tree
this.ClientSize = new System.Drawing.Size(176, 180);
this.Controls.Add(this.treeView1);
this.Menu = this.mainMenu1;
this.Text = "Explorer";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Tree_Closing);
this.Load += new System.EventHandler(this.Tree_Load);

}

#endregion

public string stripExtraSlash(string str)
{
string path =String.Empty;
if ( str.Length > 1 && str.StartsWith(@"\"))
{
path = str.Substring( 2, str.Length - 2);
}
else
{
path = str;
}
return path;
}

private void displayChildNodes (System.Windows.Forms.TreeNode parentNode )
{
DirectoryInfo FS = new DirectoryInfo(stripExtraSlash (parentNode.FullPath));


try {
foreach (DirectoryInfo dirInfo in FS.GetDirectories() )
{
//' ?-create a new node ?-
TreeNode node = new TreeNode();
node.Text = dirInfo.Name;
node.ImageIndex = icoClose;
node.SelectedImageIndex = icoOpen;
parentNode.Nodes.Add(node);
// ?-add the dummy node?-
node.Nodes.Add("");
}
}
catch (Exception err)
{
System.Windows.Forms.MessageBox.Show(err.Message);
}

try {
foreach (FileInfo fileInfo in FS.GetFiles() )
{
// create a new node to be added
TreeNode node = new TreeNode();
node.Text = fileInfo.Name;
node.ImageIndex = icoFile;
node.SelectedImageIndex = icoFile;
parentNode.Nodes.Add(node);
}
}
catch( Exception err)
{
System.Windows.Forms.MessageBox.Show(err.Message);
}
}


private void Tree_Load(object sender, EventArgs e)
{

TreeNode node=new TreeNode();
try
{
node.ImageIndex = icoClose;
node.SelectedImageIndex = icoOpen;
node.Text = @"\";
treeView1.Nodes.Add(node);

node.Nodes.Add("");
treeView1.SelectedNode = node;
}
catch (Exception err)
{
}
}

private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{

if( e.Node.ImageIndex == icoFile) return;

// remove the dummy node and display the subdirectories and files
try {
e.Node.Nodes.Clear(); // clears all the nodes and...
displayChildNodes(e.Node); // create the nodes again
}
catch(Exception err )
{
System.Windows.Forms.MessageBox.Show(err.Message);
}


if (e.Node.GetNodeCount(false) > 0) {
e.Node.ImageIndex = icoClose;
e.Node.SelectedImageIndex = icoOpen;
}

}

private void menuItem2_Click(object sender, EventArgs e)
{

}
private void mnuCopy_Click(object sender, EventArgs e)
{

// copy
pathName = stripExtraSlash(treeView1.SelectedNode.FullPath);
fileName = treeView1.SelectedNode.Text;
copy = true;
mnuPaste.Enabled = true;
}

private void menuCut_Click(object sender, EventArgs e)
{
pathName = stripExtraSlash(treeView1.SelectedNode.FullPath);
fileName = treeView1.SelectedNode.Text;
copy = false;
mnuPaste.Enabled = true;
treeView1.SelectedNode.Remove();
}

private void mnuPaste_Click(object sender, EventArgs e)
{
File.Copy(pathName, stripExtraSlash(treeView1.SelectedNode.FullPath) + @"\" + fileName, true);
System.Windows.Forms.TreeNode node = new System.Windows.Forms.TreeNode();
node.Text = fileName;
node.ImageIndex = icoFile;
node.SelectedImageIndex = icoFile;
treeView1.SelectedNode.Nodes.Add(node);

if (!copy){
File.Delete(pathName);
}
mnuPaste.Enabled = false;
}

private void mnuDelete_Click(object sender, EventArgs e)
{
File.Delete(stripExtraSlash(treeView1.SelectedNode.FullPath));
treeView1.SelectedNode.Remove();
}

private void menuItem1_Click(object sender, EventArgs e)
{
this.Close();
}

private void Tree_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.OnNotifyParent();
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
this.pathName = stripExtraSlash(treeView1.SelectedNode.FullPath);
}
}
}


private void menuItem2_Click(object sender, EventArgs e)
{
Explorer.Tree exp = new Explorer.Tree();
exp.NotifyParent += new EventHandler(tree_NotifyParent);
exp.Show();
}

private void tree_NotifyParent(object sender, EventArgs e)
{
this.textBox1.Text= sender.ToString();
if (this.textBox1.Text != String.Empty)
{
System.Drawing.Bitmap bmp = new Bitmap(this.textBox1.Text);
this.pictureBox1.Image = bmp;
this.pictureBox1.Show();
}
}

2 Comments:

Anonymous Anonymous said...

what r these foolish people doing

5:33 PM  
Anonymous Anonymous said...

what r these foolish people doing

5:34 PM  

Post a Comment

<< Home

Google
 
Web hardiks.blogspot.com