Saturday, May 30, 2009

BUG SOLVE: AcceptButton not Selected when Form is loaded

// BUG SOLVE: AcceptButton not Selected when Form is loaded

private void Form1_Load(object sender, EventArgs e)
{
if (AcceptButton != null)
((Button)AcceptButton).Select();
}

Local protect data (bind to a machine)

// Local protect data (bind to a machine)

using System.Security.Cryptography;

private static readonly byte[] salt = new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c };

public static byte[] ProtectLocalData(byte[] plain)
{
return ProtectedData.Protect(plain, salt, DataProtectionScope.LocalMachine);
}

public static byte[] UnprotectLocalData(byte[] cipher)
{
return ProtectedData.Unprotect(cipher, salt, DataProtectionScope.LocalMachine);
}

Dataset compression

// Dataset compression

private static byte[] DataSetCompress(DataSet dataSet)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
bf.Serialize(ds, dataSet);
ds.Flush();
ds.Close();
return ms.ToArray();
}
private static DataSet DataSetDecompress(byte[] data)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
DataSet dataSet = (DataSet)bf.Deserialize(ds);
return dataSet;
}

Safe update of windows control from other threads

// Safe update of windows control from other threads

delegate void UpdateReportCallback(string text);
private void UpdateReport(string message)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBoxReport.InvokeRequired)
{
UpdateReportCallback d = new UpdateReportCallback(UpdateReport);
this.Invoke(d, new object[] { message });
}
else
{
textBoxReport.Text = message + System.Environment.NewLine + textBoxReport.Text;
}
}

Parametrized thread start

// Parametrized thread start

Thread t = new Thread (new ParameterizedThreadStart(FetchUrl));
t.Start (www.google.com);

// ....

static void FetchUrl(object _url)
{
string url = (string)_url;
}

Access list of digital certificates through built-in UI

// Access list of digital certificates through built-in UI

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certs = X509Certificate2UI.SelectFromCollection(store.Certificates,"Certificates", "Please select certificate to use", X509SelectionFlag.SingleSelection);

Primitive run count application (demo) protection

// Primitive run count application (demo) protection

public static int GetRunCount()
{
int count = 0;
string guid = "";
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes\Software\Settings", true);

if (regKey != null)
{
string temp = (string)regKey.GetValue("Options");
guid = (string)regKey.GetValue("Guid");
count = (int)(temp[0] ^ guid[0]);
}

return count;
}
public static int IncrementRun()
{
int count = 0;
string guid = "";
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes\Software\Settings", true);

if (regKey == null)
{
regKey = Registry.CurrentUser.CreateSubKey(@"Software\Classes\Software\Settings");
guid = Guid.NewGuid().ToString();
regKey.SetValue("Guid", guid);
count = 0;
}
else
{
string temp = (string)regKey.GetValue("Options");
guid = (string)regKey.GetValue("Guid");
count = (int)(temp[0] ^ guid[0]);
}

Random rnd = new Random();
count++;
string value = string.Format("{0}{1}", (char)(guid[0] ^ count), GenerateGarbage(15));

regKey.SetValue("Options", value);

return count;
}
private static string GenerateGarbage(int length)
{
string retVal;
if (length < 0)
retVal = null;
else if (length == 0)
retVal = "";
else
{
Random rnd = new Random();
StringBuilder str = new StringBuilder();

for (int i = 0; i < length; i++)
str.Append((char)rnd.Next(33, 126));
retVal = str.ToString();
}
return retVal;
}