A snippet is a small section of text or source code that can be inserted into the code of a program. Snippets provide an easy way to implement commonly used code or functions into a larger section of code. Instead of rewriting the same code over and over again, a programmer can save the code as a snippet and simply drag and drop the snippet wherever it is needed. By using snippets, programmers and Web developers can also organize common code sections into categories, creating a cleaner development environment. Snippets used in software programming often contain one or more functions written in C#, VB.NET, Java, or any other programming language.
Generate MD5 Hash
Helper:
public static string GenerateMD5Hash(string password)
{
byte[] textBytes = System.Text.Encoding.Unicode.GetBytes(password);
try
{
System.Security.Cryptography.MD5CryptoServiceProvider cryptWrapper;
cryptWrapper = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash = cryptWrapper.ComputeHash(textBytes);
string ret = "";
foreach (byte a in hash)
{
ret += a.ToString("x2");
}
return ret.ToUpper(); ;
}
catch
{
throw;
}
}
Implementation:
GenerateMD5Hash("mystringvalue");
Retrieve MD5 Hash Value From A File
Helper:
public static string GetMD5HashValue(string pathName)
{
string strResult = "";
string strHashData = "";
byte[] arrbytHashValue;
System.IO.FileStream oFileStream = null;
System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher=
new System.Security.Cryptography.MD5CryptoServiceProvider();
try
{
oFileStream = GetFileStream(pathName);
arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream);
oFileStream.Close();
strHashData = System.BitConverter.ToString(arrbytHashValue);
strHashData = strHashData.Replace("-", "");
strResult = strHashData;
}
catch(System.Exception ex)
{
throw;
}
return(strResult);
}
Implementation:
GetMD5HashValue(@"c:\temp\data.txt");
Get the name of the current page
Helper:
public static string GetCurrentPageName()
{
string path = HttpContext.Current.Request.Url.AbsolutePath;
FileInfo info = new FileInfo(path);
string pageName = info.Name;
return pageName;
}
Implementation:
GetCurrentPageName();
Get the physical application path
Helper:
public static string GetPhysicalApplicationPath(HttpContext context)
{
string _applicationPath = string.Empty;
_applicationPath = context.Request.PhysicalApplicationPath;
return _applicationPath;
}
Implementation:
GetPhysicalApplicationPath(ctx);
Get the application path
Helper:
public static string GetApplicationPath()
{
string _applicationPath = HttpContext.Current.Request.ApplicationPath.ToLower();
if (Convert.ToBoolean(string.Compare(_applicationPath, "/", true))) //a site
_applicationPath = "/";
else if (!_applicationPath.EndsWith(@"/")) //a virtual
_applicationPath += @"/";
return _applicationPath;
}
Implementation:
GetApplicationPath();
Does the browser support javascript
Helper:
public static bool DoesBrowserSupportJavaScript()
{
Version ver = new Version();
if (IsHttpContext)
ver = HttpContext.Current.Request.Browser.EcmaScriptVersion;
if (ver.Major > 1)
return true;
return false;
}
Implementation:
DoesBrowserSupportJavaScript();
Find control recursively
Helper:
public static Control FindControlRecursive(Control ctrl, string id)
{
// Exit if this is the control we're looking for
if (ctrl.ID == id)
return ctrl;
else
{
// look in the hiearchy.
foreach (Control childCtrl in ctrl.Controls)
{
Control resCtrl = FindControlRecursive(childCtrl, id);
// Exit if we've found the result
if (resCtrl != null)
return resCtrl;
}
}
return null;
}
Implementation:
FindControlRecursive(txtBox, firstName);







Recent Comments