Archive

Author Archive

UAD6 – Physical path to virtual

December 7th, 2010 No comments

Simple util to take a physical path and gets its virtual equivalent.

public static string PhysicalPathToVirtual(string path)
        {
            if (HttpContext.Current != null && !String.IsNullOrEmpty(path))
            {
                return "/" + path.Replace(HttpContext.Current.Server.MapPath("/"), "").Replace(@"\", "/");
            }
            else return null;
        }
        public static string PhysicalPathToVirtual(HttpContext context, string path)
        {
            if (context != null && !String.IsNullOrEmpty(path))
            {
                return "/" + path.Replace(context.Server.MapPath("/"), "").Replace(@"\", "/");
            }
            else return null;
        }
 
 <a href="http://www.fruitbatscode.com/net/c/uad6-physical-path-to-virtual#more-433" class="more-link">Read more...</a>
Categories: Asp.Net, c# Tags:

UAD5 – Append Directory

December 3rd, 2010 No comments

When creating various file explorer for asp.net I generate thumbnails and put these in a hidden thumbs folder. This little routine just slips in a directory at the end so c:\images\file.jpg becomes c:\images\thumbs\file.jpg.

/// <summary>
/// Adds the passed directory into the string
/// </summary>
/// <param name="filePath">Original Path</param>
/// <param name="directoryName">directory name to append</param>
/// <returns>The new path</returns>
public static String AppendDirectory(string filePath, string directoryName)
{
	return Path.Combine(
		Path.Combine(Path.GetDirectoryName(filePath), directoryName)
		, Path.GetFileName(filePath)
		);
}
Categories: c# Tags:

UAD4 – Is Valid Email

December 1st, 2010 No comments

A simple regex to see if a string is in a valid email format

public static bool IsValidEmail(string email)
{
    string expression = @"^(([^<>()[\]\\.,;:\s@\""]+"
        + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
        + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
        + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
        + @"[a-zA-Z]{2,}))$";
    Regex re = new Regex(expression);
    if (re.IsMatch(email))
        return (true);
    else
        return (false);
 
 <a href="http://www.fruitbatscode.com/net/c/uad4-is-valid-email#more-426" class="more-link">Read more...</a>
Categories: c# Tags:

UAD3 – Enum to ListItemCollection

November 30th, 2010 2 comments

Converts an enum to a ListItemCollection so users can select an option from an enum. Just cast it back like so:

(enumToUse)Enum.Parse(typeof(enumToUse), DropDownList.SelectedItem.Value)
        /// <summary>
        /// Converts enum to ListItemCollection
        /// </summary>
        /// <param name="EnumToUse">Type of enum to use</param>
        /// <param name="ListItems">Target LIstItemCollection</param>
		public static void EnumToListItemCollection(System.Type EnumToUse, ListItemCollection ListItems)
		{
			string[] Names;
			System.Array Values;
			Int32 index;
 
 <a href="http://www.fruitbatscode.com/net/c/uad3-enum-to-listitemcollection#more-421" class="more-link">Read more...</a>
Categories: c# Tags:

UAD2 – Label Change

November 25th, 2010 No comments

Server controls are great but sometimes you need to change labels for different users, like change ‘Product Name’ to ‘Book Title’ when all they sell is books. You can create a load of properties or just use a simple util like this:

/// <summary>
        /// Loops through labels in a control swapping matching values
        /// </summary>
        /// <param name="items">Dictionary where Key is the value to Search for and Value is wht to replace it with</param>
        public static void LabelChange(ControlCollection controls, Dictionary<String, String> items)
        {
            foreach (Control c in controls)
            {
                if (c is Label)
                {
                    Label lbl = c as Label;
                    if (items.ContainsKey(lbl.Text))
                    {
                        lbl.Text = items[lbl.Text];
                    }
                }
            }
        }
Categories: c# Tags:

Utility Function A Day.

November 24th, 2010 No comments

I have a little plan to release a small code snippet a day till I run out or get bored ;) Just simple little utility functions that do things like check phone numbers, credit cards etc. Nothing special but just handy to have a list around!

Heres the first:
Does the string contain Html? Read more…

Categories: c# Tags: