Brian Dorey.com

Random Stuff and Projects

ASP.NET C# Code

Here are some useful functions written in c#

Left and Right functions:

public static string Right(string strParam,int iLen)
{
if(iLen>0)
return strParam.Substring(strParam.Length-iLen,iLen);
else
return strParam;
}

public static string Left(string strParam,int iLen)
{
if(iLen>0)
return strParam.Substring(0,iLen);
else
return strParam;
}

Regex check valid email:

bool IsValidEmail(string strIn)
{
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}

File size format:

public string DoSizeFormat(string invalue) {
double filesize = System.Convert.ToDouble(invalue);
string displayvalue = "";
if (filesize >= 1073741824) {
displayvalue = (filesize / 1024 / 1024 / 1024).ToString("F") +" GB";
}
if (filesize >= 1048576) {
displayvalue = (filesize / 1024 / 1024).ToString("F") +" MB";
}
if (filesize >= 1024) {
displayvalue = (filesize / 1024).ToString("F") +" KB";
}
if (filesize < 1024) {
displayvalue = (filesize).ToString("F") +" B";
}
return displayvalue;

}

Date Format:

public string DoDateFormat(string inval) {
try {
if (inval.Length > 0) {
return DateTime.Parse(inval).ToString("dd/MM/yyyy");
} else {
return inval;
}
} catch {
return "Error with Value";
}
}

Currency Format:

public string DoCurrencyFormat(string inval) {
try {
if (inval.Length > 0) {
return Double.Parse(inval).ToString("C");
} else {
return inval;
}
} catch {
return "Error with Value";
}
}