Blog moving to http://blog.xploiter.com

15 01 2009

This blog will be moving, actually it already has, i’m just keeping the two side by side until the search engines grab a hold of it properly.

Anyways the new URL is:

http://blog.xploiter.com

Pass it around, add comments, use and abuse it and let me know what you want me to cover 🙂

Si

Advertisements




How to programmatically turn on the Numlock Key

7 11 2008

The following code will allow you to turn on the numlock key upon startup. Can’t credit anyone as i’m not sure where it came from but it does work 100%

using System;
using System.Runtime.InteropServices;

class SetNumlockKeyOn
{
[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
internal int type;
internal short wVk;
internal short wScan;
internal int dwFlags;
internal int time;
internal IntPtr dwExtraInfo;
int dummy1;
int dummy2;
internal int type1;
internal short wVk1;
internal short wScan1;
internal int dwFlags1;
internal int time1;
internal IntPtr dwExtraInfo1;
int dummy3;
int dummy4;
}
[DllImport(“user32.dll”)]
static extern int SendInput(uint nInputs, IntPtr pInputs, int cbSize);

public static void SetNumlockOn()
{
const int mouseInpSize = 28;//Hardcoded size of the MOUSEINPUT tag !!!
INPUT input = new INPUT();
input.type = 0x01; //INPUT_KEYBOARD
input.wVk = 0x90; //VK_NUMLOCK
input.wScan = 0;
input.dwFlags = 0; //key-down
input.time = 0;
input.dwExtraInfo = IntPtr.Zero;

input.type1 = 0x01;
input.wVk1 = 0x90;
input.wScan1 = 0;
input.dwFlags1 = 2; //key-up
input.time1 = 0;
input.dwExtraInfo1 = IntPtr.Zero;

IntPtr pI = Marshal.AllocHGlobal(mouseInpSize * 2);
Marshal.StructureToPtr(input, pI, false);
int result = SendInput(2, pI, mouseInpSize); //Hardcoded size of the MOUSEINPUT tag !!!

//if (result == 0 || Marshal.GetLastWin32Error() != 0)
// Console.WriteLine(Marshal.GetLastWin32Error());
Marshal.FreeHGlobal(pI);
}
}





Deleting elements from XML Documents

7 11 2008

If you need to delete either a Node, Element or Attribute from your XML document, try the following code as it actually works!!!

XML Document Structure

<?xml version=1.0encoding=utf-8standalone=yes?>
<!–
XML Config file–>
<Tests
>
<
Test>
<
ID>47d272cd-877d-4d0f-ad4a-cfa31200d966</ID>
<
Object>Obj1</Object>
<
TestType>Fail Yes</TestType>
<
Command>fail yes</Command>
<
Time>202</Time>
<
Expiry>0</Expiry>
</
Test>
<
Test>
<
ID>71b211a7-99e4-4041-ada7-2925b6aff46f</ID>
<
Object>Obj2</Object>
<
TestType>Fail No</TestType>
<
Command>fail no</Command>
<
Time>243</Time>
<
Expiry>0</Expiry>
</
Test>
</
Tests>

To delete the element based on the GUID I Use:

///
/// Allows you to delete an element from the XML File
///
/// String GUID ID to remove from the config file
/// Name of the element to delete
public void RemoveElement(string elementName, string id)
{
System
.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.Load(filename);

System.Xml.XmlNodeList list = doc.GetElementsByTagName(Test);

for (int i = 0; i < list.Count; i++)
{

int j = 0;
for (j = 0; j < list[i].ChildNodes.Count; j++)
{

if (list[i].ChildNodes[j].Name.ToUpper() == “ID”)
break;
}

if (j < list[i].ChildNodes.Count && list[i].ChildNodes[0].InnerText == id)
{
System
.Xml.XmlNode parent = list[i].ParentNode;
parent
.RemoveChild(list[i]);
}
}
doc
.Save(filename);
}

Another way that i’ve not got around to testing (but which I like) is to use a DataView to suck the XML in, parse using the filter property and then after deleting, re-export as XML:

DataSet ds = new DataSet();
ds
.ReadXml(“xmlAdd file as above”);
if (ds != null && ds.Tables.Contains(“stickies”))
{

DataView dv = new DataView(ds.Tables[“stickies”]);
dv
.RowFilter = “id=1”;
if (dv.Count >= 1)
{
dv[
0].Delete();
ds
.AcceptChanges();
ds
.WriteXml(“updated.xml”);
}
}





Using Regular Expressions to see if string validates as a numeric or number value

7 11 2008

The following method will take in a string and return a bool value if it’s a valid number:

///         
/// Uses Regular expressions to see if we are a number        
///         
///         
///         
static bool IsNumeric(string inputString)
{

return System.Text.RegularExpressions.Regex.IsMatch(inputString, “^[0-9]+$”);
}





Using KeyPreview to handle form shortcut keys

31 10 2008

If you need to trap shortcut keys in your application to perform misc tasks, use the following code to set KeyPreview to true and then in the KeyDown event handle the actual clicks:

 private void Form_OnLoad(object sender, EventArgs e)
{
KeyPreview
= true;
}

private void form_KeyDown(object sender, KeyEventArgs e)
{

if (e.Alt && e.KeyCode.ToString() == “L”)
{

//DO Something            
}
}





SQL Datetime Woes – The ultimate guide to the datetime datatypes

3 10 2008

I’ve not worked with SQL Server DateTime values for a while now so was a little rusty. I needed to do some date filtering and came across the following site by MVP Tibor Karaszi that I found really useful:

http://www.karaszi.com/SQLServer/info_datetime.asp





Dotnet Charting

17 09 2008

I was looking for some suitable dotnet charting components/code and came across the following three articles.

http://www.c-sharpcorner.com/UploadFile/pseabury/2DCharting11152005052210AM/2DCharting.aspx

http://www.c-sharpcorner.com/UploadFile/steve_hall/drawinglinechart02072007053420AM/drawinglinechart.aspx

http://www.c-sharpcorner.com/UploadFile/jodonnell/ChartingInGDIplus11192005063716AM/ChartingInGDIplus.aspx

Heres a screenshot of the one i’m using: