Oct 6
/// <summary>
/// Round up to the best human readable number
/// (could be kb, mb or gb depending on size of
/// number
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public string BytesToHumanReadable(double bytes)
{
var gig = Math.Pow(1024, 3);
var meg = Math.Pow(1024, 2); if(bytes > gig)
return Math.Round(bytes / gig, 1) + " GB";
else if (bytes > meg)
return (int)(bytes / meg ) + " MB";
else
return (int)(bytes / 1024 ) + " KB";
}
This is useful for file and memory sizes.
Sep 11
http://blog.sqlauthority.com/2006/12/30/sql-server-shrinking-truncate-log-file-log-full/
Assuming SMS is your database name and sms_log is your transaction log name:
USE SMS
GO
DBCC SHRINKFILE(sms_log, 1)
BACKUP LOG sms WITH TRUNCATE_ONLY
DBCC SHRINKFILE(sms_log, 1)
Sep 11
If you need to find the size of each table in the database here is an easy way.
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
I use it to find those pesky tables that are hogging all my disk space. (Usually it's the log file though)
Source:
http://www.4guysfromrolla.com/webtech/032906-1.shtml
Jul 30
This method indicates whether an object is numeric or not. Handy in reflection type situations when you need to perform and action based on type.
private static bool IsNumeric(Object o)
{
if((o is int) || (o is int?) ||
(o is decimal) || (o is decimal?) ||
(o is double) || (o is double?) ||
(o is float) || (o is float?) ||
(o is long) || (o is long?) ||
(o is ulong) || (o is ulong?) ||
(o is ushort) || (o is ushort?) ||
(o is short) || (o is short?) ||
(o is byte) || (o is byte?) ||
(o is sbyte) || (o is sbyte?) ||
(o is uint) || (o is uint?))
return true;
return false;
}
It's long and a bit ugly but it's the only way I've found.
Jul 13
http://erikengbrecht.blogspot.com/2008/07/love-hate-and-type-inference.htmlType inference is great, locally that is. When it's global it's a mess. That's why large programs are easier to maintain and refactor in C# than in Python.
Conversely, scripts are much better in Python than in C#.
So when right a short and dirty script, Python is fantastic. When writing something a little bigger, the statically typed languages start to win. There is no "Static is better or Dynamic is better" only what locally optimal.
I think C# 3.0 is blurring the line somewhat with the best of both worlds. It's locally type inferred (or it can be if you want it that way) and globally static.
Jul 13
Scraping the web is hard.
Matt Cutts says so:
http://www.mattcutts.com/blog/the-web-is-a-fuzz-test-patch-your-browser-and-your-web-server/
I've found this to be true.
A couple of implications.
It's hard to build a web crawler that can suck information out of pages reliably.
Validation doesn't matter b/c google doesn't penalize for it. And if Google doesn't care, you shouldn't either.
Jun 12
Occasionally I need to change a a camel case or Pascal case string into a human readable string. Since I find myself doing this again and again, I figured I better post it here.
private string HumanizeString(string source)
{
StringBuilder sb = new StringBuilder(); char last = char.MinValue;
foreach (char c in source)
{
if (char.IsLower(last) &&
char.IsUpper(c))
{ sb.Append(' '); }
sb.Append(c);
last = c;
}
return sb.ToString();
}
Mar 26
Find controls in a page or another control
List<T> FindControls<T>(Control control) where T : Control
{
List<T> list = new List<T>(); foreach (Control c in control.Controls)
{
if (c is T)
{
Global.LogDebug(c.ID);
list.Add(c as T);
}
list.AddRange(FindControls<T>(c));
}
return list;
}
private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
Mar 26
Find the compatible types in an assembly:
public static Type[] FindCompatibleTypes(Assembly assembly, Type baseType)
{
List<Type> types = new List<Type>(); foreach (Type type in assembly.GetTypes())
{
if (type != baseType && baseType.IsAssignableFrom(type))
types.Add(type);
}
return types.ToArray();
}