www.kevincornwell.com

.NET



.NET: Convert A String Into A Compressed String

Written with .NET 2.0. Should work with 3.x as well.

using System.IO.Compression;
using System.Text;
 
protected string Compress(string text)
{
    byte[] buffer = Encoding.UTF8.GetBytes(text);
    MemoryStream ms = new MemoryStream();
    using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
    {
        zip.Write(buffer, 0, buffer.Length);
    }
 
    ms.Position = 0;
    MemoryStream outStream = new MemoryStream();
 
    byte[] compressed = new byte[ms.Length];
    ms.Read(compressed, 0, compressed.Length);
 
    byte[] gzBuffer = new byte[compressed.Length + 4];
    System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
    System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
    return Convert.ToBase64String(gzBuffer);
}
 
 
protected string Decompress(string compressedText)
{
    byte[] gzBuffer = Convert.FromBase64String(compressedText);
    using (MemoryStream ms = new MemoryStream())
    {
        int msgLength = BitConverter.ToInt32(gzBuffer, 0);
        ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
 
        byte[] buffer = new byte[msgLength];
 
        ms.Position = 0;
        using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
        {
            zip.Read(buffer, 0, buffer.Length);
        }
 
        return Encoding.UTF8.GetString(buffer);
    }
}

Post Comment Now


.net versions rant

I am fairly certain the .NET versioning (numbering release) scheme is the dumbest ever created. Seriously who in their right mind does a point release that is a extension of the previous point but not the one before that (2.0 -> 3.0)? Then 3.0 -> to 3.5? wtf? Remember 1.0 and 1.1? Hmmm. How does that fit in? Oh yea, it doesn’t.

It went like this…

1.0 to 1.1. Perfect. Basically the 1.0 framework fixed with a couple new things. That’s actually how it took place.

Then 2.0. A whole new framework. Yay! We are good at this point.

Then 3.0. Huh? Another new framework? Yay!? Wait, no, huh? It’s not a framework? It’s a extension but only for 2.0 and not related at all to 1.0 or 1.1? Oh good heavens MS.

Then 3.5 Another extension to 2.0 or actually 3.0? Ok, now I’m getting mad. What happened to 3.1, 3.2, etc.?

What’s next? 8.0 with 10.0 extentions?

Might as well number it 38.5. What’s the difference at this point? There is NO consistency anyway and the higher the number the better right?

Anyway here is the 2.0 -> 3.5 relationships…
dotnetframework35.png

Seriously, if you know what the hell MS was thinking, post a comment.


Post Comment Now


ASP.NET - Test NTFS Directory Access
    protected void Page_Load(object sender, EventArgs e)
    {
 
        // Show admin link if they have access to the admin directory. //
 
        hyperLinkAdmin.Visible = hasAccessToDir("admin");
    }
 
    protected bool hasAccessToDir(string dir)
    {
        return System.IO.Directory.Exists(MapPath(dir));
    }

Post Comment Now


Create Active Directory User In .NET

C#

    public string CreateADUser(string szUsername, string szDisplayName, string szFirstName, string szLastName, string szMiddleInitial, string szPhone, string szEmail, string szOrgUnit)
    {
        string szPassword = "fooBar_123";  //temp only.
        string szCNName = "CN=" + szUsername;
 
        try
        {
            DirectoryEntry objContainer = new DirectoryEntry("LDAP://OU=mySubOU,OU=myMainOU,DC=KEVINCORNWELL,DC=com");
            DirectoryEntry objUser = objContainer.Children.Add(szCNName.ToLower(), "user");
            objUser.Properties["sAMAccountName"].Value = szUsername;
            objUser.Properties["userPrincipalName"].Value = szUsername + "@KEVINCORNWELL.COM";
            objUser.Properties["displayName"].Value = szDisplayName;
            objUser.Properties["GivenName"].Value = szFirstName;
            objUser.Properties["sn"].Value = szLastName;
            objUser.Properties["Initials"].Value = szMiddleInitial;
            objUser.Properties["mail"].Value = szEmail;
            objUser.Properties["TelephoneNumber"].Value = szPhone;
            objUser.Properties["Description"].Value = szOrgUnit;
            objUser.CommitChanges();
            objUser.Invoke("SetPassword", szPassword);
            objUser.CommitChanges();
            int flags = (int)objUser.Properties["userAccountControl"].Value;
            objUser.Properties["userAccountControl"].Value = flags & ~0x2;  //Enable User Account
            objUser.Properties["pwdLastSet"].Value = 0;
            objUser.CommitChanges();
            return "";
        }
        catch (Exception ex)
        {
            return ex.InnerException + ex.Message + ex.Source + ex.StackTrace + ex.Data;
        }
    }

Post Comment Now


DD-WRT MAC Address Toolkit

This .NET 2.0 website adds additional functionality to associate a name and notes to the MAC addresses in a DD-WRT flashed router. I did this because the built in only has MAC fields which makes it cumbersome to associate names/computer to MACs when you have dozens of clients. The C# source code is included in the zip file. I make use of the open source SharpSSH library to gain access to the router over SSH.

You will have to edit several files for your particular environment.

Screenie

DD-WRT MAC Address Toolkit Website


Post Comment Now


Blog | Contact | Gallery | Links | Music | Sandbox | Search | SharePoint | Social Networking | Software | Weather | Web Design


Copyright © 1997-2008 KCSH. All rights reserved.