Coding

This is where I keep my snippets

  • C# Retrieve MAC Address with WMI

    We use WMI in System.Management Here is a .NET Framework 2.0 way of doing object queries in this case: ManagementObjectSearcher query = new ManagementObjectSearcher(“SELECT * FROM Win32_NetworkAdapterConfiguration”); ManagementObjectCollection queryCollection = query.Get(); string mac = “”; foreach (ManagementObject mo in queryCollection) { try { mac = mo[“MacAddress”].ToString(); MessageBox.Show(mac); } catch { } }

    read more

  • Merging Layers in Acrobat 8

    So if you find you have a whole bunch of layers and you need to repeatedly throw them all together all the time, you can do this with javascript: //Open Javascript Console console.show(); var MAINocgLayerName = “Add Boundary Sets”; var mergedOCGArray = new Array(); var sourceOCGArray = this.getOCGOrder(); var OCGArray = this.getOCGs(); var sourceLayer; var

    read more

  • C# Unit Testing in a nutshell

    OK so there’s a thing called test-based development techniques. Essentially it means that if you want to write a piece of code, you first write a test piece which defines an example of input variables and corresponding output varaibles. It then calls the function you want to write and retrieves the ouput from that function.

    read more

  • C# Random number

    To get basic semi-random numbers: Random random = new Random(); int n = random.Next(min, max);

    read more

  • C# Write to File

    Writing to a file is relatively easy in C#. ///<summary> ///Write an entry to our logfile</summary> ///<param name=”message”>The message you want to write to the log file</param> private voidlog(string message) { StreamWriter writer = newStreamWriter(@”c:\log.txt”, true); writer.WriteLine(formatEntry(message)); writer.Flush(); writer.Close(); } Simple.

    read more

  • C# Get Logged in User

    System.Environment.GetEnvironmentVariable(“USERNAME”);

    read more

  • C# background processing

    Here’s a quick reminder on how to not tie up you form thread while doing time consuming operations from controls on a form. .NET makes this relatively easy: 1. Declare your delegate: private delegate void CallAsyncWorkerDirectDelegate(string func, string param); 2. Write your Worker function private void CallAsyncWorkerDirect(string func, string param) { string parm1, parm2; parm1

    read more