Sunday, December 16, 2007

Best plugin for publishing code snippets via Windows Live Writer

After testing a couple of different code snippet publishing plugins for Windows Live Writer (WLW), I found that Leo Vildosola's plugin to work best with Google's Blogger.

If you select the embedded option - the code is published properly colored and formatted with indentations and all.

Download from WLW Plugin Gallery

A couple of other useful plugins for developers:

Here is some sample code pasted via Leo's "Insert Code Snippet" plugin

// NOTE: This code snippet is designed for VS.NET 2005 or later.
// Additional reference: System.Security.dll
// using using System.Security.Cryptography;
// using System.IO;
// This is a button click event - to encrypt the DB string to disk:
private void btnEncrypt_Click(object sender, EventArgs e)
{
    // The DB string that we want to encrypt/decrypt:
    String strConnectionString = $strDBString$;
    // Call the custom method for encrypting the string:
    this.EncryptDBString(strConnectionString);
}

// This is  a button click event - to unencrypt the saved DB string:
private void btnDecrypt_Click(object sender, EventArgs e)
{
    String strConnectionString = this.DecryptDBString();
    // Do something with the unencrypted DB string ...
    MessageBox.Show(strConnectionString);
}

// This method will encrypt the provided DB string to disk
private void EncryptDBString(String DBString)
{
    /**************** Encrypt/Protect Database String ****************/
    // Convert the string to a byte array:
    byte[] arrDBString = System.Text.Encoding.Unicode.GetBytes(DBString);
    // Provide additional protection via entropy with another byte array:
    byte[] arrEntropy = { 4, 5, 7, 9, 4, 5, 7, 9 }; // Save this for unprotecting later
    // Encrypt/protect the DB string:
    byte[] arrEncryptedDBString = ProtectedData.Protect(arrDBString, arrEntropy,
        DataProtectionScope.CurrentUser);

    // Write the encrypted DB string to disk:
    using (FileStream fs = new FileStream($strFileName$, FileMode.OpenOrCreate))
    {
        fs.Write(arrEncryptedDBString, 0, arrEncryptedDBString.Length);
    }
}

// This method will decrypt the saved encrypted DB string
private String DecryptDBString()
{
    /**************** Decrypt/Unprotect Database String ****************/
    // Setup the unencrypted DB string to return:
    String strUnencryptedDBString = null;
    // Provide additional protection via entropy with another byte array:
    byte[] arrEntropy = { 4, 5, 7, 9, 4, 5, 7, 9 }; // Save this for protecting later
    // Setup the byte array that will hold the encrypted DB string:
    byte[] arrEncryptedDBString = new byte[0];
    // Read the encrypted DB string from disk:
    if (File.Exists($strFileName$))
    {
        using (FileStream fs = new FileStream($strFileName$, FileMode.Open))
        {
            // Reset the byte array's length based on read length:
            arrEncryptedDBString = new byte[fs.Length];
            // Read the encrypted file into the byte array:
            fs.Read(arrEncryptedDBString, 0, (int) fs.Length);
        }
    }

    if (arrEncryptedDBString.Length > 0)
    {
        // Decrypt/unprotect the DB string:
        byte[] arrUnencryptedDBString = ProtectedData.Unprotect(arrEncryptedDBString,
            arrEntropy, DataProtectionScope.CurrentUser);
        // Convert the byte array to a string:
        strUnencryptedDBString = System.Text.Encoding.Unicode.GetString(arrUnencryptedDBString);
    }
    // Return the unencrypted DB string:
    return strUnencryptedDBString;
}
            

No comments: