Friday, June 13, 2008

Uninstall application from code - C#

For Window CE:

RegistryKey rk = Registry.LocalMachine;
rk = rk.OpenSubKey(@"\SOFTWARE\Apps\Socket Bluetooth OBEX Software",true);
rk.DeleteValue("Instl", false);
rk.DeleteValue("InstlDirCnt", false);
rk.DeleteValue("InstlDir", false);
rk.Close();

rk = Registry.LocalMachine;
rk = rk.OpenSubKey(@"\SOFTWARE\Apps\Socket Bluetooth Software", true);
rk.DeleteValue("Instl", false);
rk.DeleteValue("InstlDirCnt", false);
rk.DeleteValue("InstlDir", false);

rk.Close();

Then check COntrol panel->Remove program, you won't see these 2 socket program.



For PC: ( Not Tested!)


Microsoft.Win32.RegistryKey Fregistry =
Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE")
.OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion")
.OpenSubKey("Installer").OpenSubKey("UserData")
.OpenSubKey("S-1-5-18").OpenSubKey("Products");
string []Names = Fregistry.GetSubKeyNames() ;
string uninstall = "";
string ApplicationName = "Adobe Reader 7.0.8";
for (int i = 0; i < Names.Length; i++)
{
Microsoft.Win32.RegistryKey FTemp = Fregistry.OpenSubKey(Names[i]).OpenSubKey("InstallProperties");
if (FTemp.GetValue("DisplayName").ToString() == ApplicationName)
{
object obj = FTemp.GetValue("UninstallString");
if (obj == null)
uninstall = "";
else
uninstall = obj.ToString();
i = Names.Length;
}
}

System.Console.WriteLine(uninstall);
System.Diagnostics.Process FProcess = new System.Diagnostics.Process();
string temp = "/x{" + uninstall.Split("/".ToCharArray())[1].Split("I{".ToCharArray())[2];
//replacing with /x with /i would cause another popup of the application uninstall
FProcess.StartInfo.FileName = uninstall.Split("/".ToCharArray())[0];
FProcess.StartInfo.Arguments = temp;
FProcess.StartInfo.UseShellExecute = false;
FProcess.Start();
System.Console.Read();

Tuesday, June 3, 2008

Treenodes and Context Meny

Make a tree structure with "Copy/paste" context menu....

private System.Windows.Forms.TreeView treeView1;
private ContextMenu cMenu = new ContextMenu();
TreeNode tmpNode, tNode;
Point pPt;

...

this.treeView1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseUp);

...

private void Form1_Load(object sender, System.EventArgs e)
{
cMenu.MenuItems.Add("Copy",new EventHandler(eCopy));
cMenu.MenuItems.Add("Paste", new EventHandler(ePaste));

TreeNode aNode;
aNode = treeView1.Nodes.Add("Dong");
aNode.Nodes.Add("Ning");
aNode.Nodes.Add("Yamei");

aNode = treeView1.Nodes.Add("Jing");
aNode.Nodes.Add("Yahong");
aNode.Nodes.Add("Ying");

treeView1.AllowDrop = true;
}
private void eCopy(object sender, System.EventArgs e)
{
// TreeNode tNode = treeView1.SelectedNode; //does not work.
tmpNode = treeView1.GetNodeAt(pPt);
tNode = tmpNode;
MessageBox.Show("Path is " + tNode.FullPath.ToString());
}
private void ePaste(object sender, System.EventArgs e)
{
//insert tNode to the pPt postion
TreeNode nodeTarget = treeView1.GetNodeAt(pPt);
TreeNode nodeSource = tNode;
MessageBox.Show("Path is " + nodeTarget.FullPath.ToString());
nodeTarget.Nodes.Add((TreeNode)tNode.Clone()); //needed
nodeTarget.Expand();
}

private void treeView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
switch(e.Button)
{
case MouseButtons.Right:
cMenu.Show(treeView1, new Point(e.X,e.Y));
pPt = new Point(e.X,e.Y);
break;
default:
break;
}
}

Monday, June 2, 2008

Directory Control: browse/delete

private void button1_Click(object sender, EventArgs e)
{
folderBrowserDialog1.SelectedPath = @"C:\My Projects\temp\DirectoryControl\Test";
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
textBox1.Text = folderBrowserDialog1.SelectedPath;
}

private void button2_Click(object sender, EventArgs e)
{
if (Directory.Exists(textBox1.Text))
{
DirectoryInfo di = new DirectoryInfo(textBox1.Text);
DeleteAllFilesInDirectories(di);
Directory.Delete(textBox1.Text, true);
}
else
{
MessageBox.Show("Invalid path!");
}
}

private void DeleteAllFilesInDirectories(DirectoryInfo di)
{
FileInfo[] fi = null;
DirectoryInfo[] subDir = null;

fi = di.GetFiles();

if (fi != null)
{
foreach (FileInfo aFi in fi)
{
//delete all
aFi.Attributes = FileAttributes.Normal;
aFi.Delete();
}
subDir = di.GetDirectories();
foreach (DirectoryInfo adi in subDir )
{
DeleteAllFilesInDirectories(adi);
}
}
}