Wednesday, November 10, 2010

Access admin group and copy it to another user group

http://support.microsoft.com/kb/306273

DirectoryEntry AD = new DirectoryEntry("WinNT://" +


Environment.MachineName + ",computer");



DirectoryEntry grp;



grp = AD.Children.Find("Power Users", "group");

if (grp != null)

{

using (DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group"))

{

foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))

{

using (DirectoryEntry memberEntry = new DirectoryEntry(member))

{

try

{

grp.Invoke("Add", new object[] { memberEntry.Path.ToString() });

}

catch { }



}

}

}

}

//add a new user group
try


{

DirectoryEntry AD = new DirectoryEntry("WinNT://" +

Environment.MachineName + ",computer");

DirectoryEntry qtmGroup = AD.Children.Add(QTM_POWER_USERS, "group");

qtmGroup.Invoke("Put", new object[] { "Description", QTM_GROUP_DESCRIPTION });

qtmGroup.CommitChanges();

}

catch { }

Find installation location for an installed app

Uninstall key
Registry.LocalMachine.DeleteSubKeyTree("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{06D45ED5-8A64-469B-8236-CB4DC0E897C9}");

Copied from
http://social.msdn.microsoft.com/Forums/en-US/winformssetup/thread/afb5012a-30f1-4b96-9931-a143fd76bab5

private string FindByDisplayName(RegistryKey parentKey, string name)


{

string[] nameList = parentKey.GetSubKeyNames();

for (int i = 0; i < nameList.Length; i++)

{

RegistryKey regKey = parentKey.OpenSubKey(nameList[i]);

try

{

if (regKey.GetValue("DisplayName").ToString() == name)

{

return regKey.GetValue("InstallLocation").ToString();

}

}

catch { }

}

return "";

}



RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");

string location = FindByDisplayName(regKey, "MSN");

MessageBox.Show(location);



This example will compare the DisplayName keyvalue to your input name, if it find the value, then return the InstallLocation key value.

Tuesday, November 9, 2010

Background worker thread

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
public partial class ProgressDialog : Form


{

public delegate void CancelEventHandler();

public event CancelEventHandler CancelButtonClick;



public ProgressDialog()

{

InitializeComponent();

}



private void buttonCancel_Click(object sender, EventArgs e)

{

if (CancelButtonClick != null)

{

CancelButtonClick();

}

this.Close();

}



public void UpdateProgressMessage(string progressMessage)

{

lblProgressMessage.Text = progressMessage;

}





}
 
private delegate void ShowProgressDialog_Delegate(string progressDialogCaption, ProgressDialog.CancelEventHandler progressCancelled, bool reportProgress);


private void ShowProgressDialog(string progressDialogCaption, ProgressDialog.CancelEventHandler progressCancelled, bool reportProgress)

{

if (InvokeRequired)

{

BeginInvoke(new ShowProgressDialog_Delegate(ShowProgressDialog), new object[] { progressDialogCaption, progressCancelled, reportProgress });

}

else

{

try

{

progressDialog = new ProgressDialog();

progressDialog.Owner = base.FindForm();

progressDialog.CancelButtonClick += progressCancelled;

progressDialog.Text = progressDialogCaption;

if (reportProgress)

{

progressDialog.progressBar.Style = ProgressBarStyle.Continuous;

}

progressDialog.Show();

}

catch { }

}



}

///

/// Close the Progress Dialog

///


private delegate void StopProgressDialog_Delegate();

private void StopProgressDialog()

{

if (InvokeRequired)

{

BeginInvoke(new StopProgressDialog_Delegate(StopProgressDialog), new object[] { });

}

else

{

if (!progressDialog.IsDisposed)

{

progressDialog.Close();

}

}



}



private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

StopProgressDialog();



if (e.Cancelled == true)

{

MessageBox.Show("Installation Cancelled!");

}

else if (e.Error != null)

{

MessageBox.Show("Installation Error: " + e.Error.Message);

}

else

{

MessageBox.Show("Installed successfully!");

}



}



void progress_CancelButtonClick()

{

TestsOptionChanged_Click(null, null);

abortFlag = true;

}