Color Of Code

Font Size

SCREEN

Profile

Layout

Menu Style

Cpanel

Miscellaneous

User Rating:  / 0
PoorBest 

Design mode

Gotcha: DesignMode property is not valid in constructor, use Load event instead.

Byte size to human readable units

 

	private static String SizeToHumanReadable (long sizeInBytes)
	{
		string[] units = { "B", "KiB", "MiB", "GiB", "TiB", "PiB" };
		double len = sizeInBytes;
		int order = 0;
		int maxOrder = units.Length - 1;
		while (len >= 1024 && order < maxOrder) {
			order++;
			len = len / 1024;
		}
		return String.Format (CultureInfo.InvariantCulture, "{0:0.000} {1}", len, units[order]);
	}

Retrieve currently logged in user

System.Security.Principal.WindowsIdentity.GetCurrent()

Debugging code

To declare code that is active for a configuration only prefer using attribute against #ifdef's for more readable code: [Conditional("DEBUG")]

For displaying user string representation of classes in the debugger, prefer using the DebuggerDisplay attribute to overriding ToString as the latter is used by String.Format primarily:

[DebuggerDisplay("Count = {count}")]
class MyHashtable
{
    public int count = 4;
}

Temporary File

using System.IO;
FileInfo tempFile = new FileInfo(Path.GetTempFileName());
...
tempFile.Delete();

Getting path of executable

Assembly.GetEntryAssembly().Location

Add comment


Security code
Refresh

You are here: Home Code Snippets C# Miscellaneous