Wednesday, November 02, 2011

Regex validate Email address C#

This is how we can validating email address through C# using Regular Expression. Here is the sample code,

using System.Text.RegularExpressions;



public bool vaildateEmail(string useremail)
{
bool istrue = false;
Regex reNum = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
CaptureCollection cc = reNum.Match(useremail).Captures;
if (cc.Count == 1)
{
istrue = true;
}
return istrue;
}

Delete All Files using C#

For Deleting all the files first you need to get the list of file names from the specified directory (using static method Directory.Get­Files. Then delete all files from the list.
Method 1
using System.IO;

string[] filePaths = Directory.GetFiles(@"c:\Directory\");
foreach (string filePath in filePaths)
  File.Delete(filePath)

Method 2

To Delete all files using one code line, you can use Array.ForEach with combination of anonymous method.

Array.ForEach(Directory.GetFiles(@"c:\MyDirectory\"),delegate(string path) 
{ File.Delete(path); });

How to get current page Filename using C#

There are different ways to get current page filename using c#. Here are 3 methods you can use for your advantage.

Method 1
string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;

Method 2

string sPath = HttpContext.Current.Request.Url.AbsolutePath;
string[] strarry = sPath.Split('/');
int lengh = strarry.Length;
string sReturnPage = strarry[lengh - 1];

Method 3

string absPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo finfo = new System.IO.FileInfo(absPath);
string fileName = finfo.Name;

 

Out of these I like Method 1 and 3 as its straight forward. Use a per your advantage. Good luck.

C# Checking a File exists in Folder

How to get all files from a folder and compare a whether that file exists in that folder or not. Here is a sample code where you can loop through the code and check the file.

I have declared a variable to check there it exists or not.

private bool isFileExists = false;


Now I have this code to check whether file exists and based on it I’ll redirect in to that page.


string searchfolder = Server.MapPath("~") + "\\TestFolder";
DirectoryInfo Dir = new DirectoryInfo(searchfolder);
FileInfo[] FileList = Dir.GetFiles("*.aspx", SearchOption.AllDirectories);
foreach (FileInfo FI in FileList)
{
if (FI.Name == redirectPage)
{
isFileExists = true;
break;
}
}
if (isFileExists == true)
{
Response.Redirect("~/redirectPage");
}
else
{
Response.Redirect("
~/OtherRedirect.aspx");
}

Here I have search pattern as *.aspx as I need to check these files. This can be based on the files you want to check

Monday, October 24, 2011

Silverlight Integration Pack for Microsoft Enterprise Library 5.0

Silverlight Integration Pack for Enterprise Library is a collection of guidance and reusable application blocks designed to assist Silverlight application developers with common LOB development challenges. This release includes: Caching Application Block, Exception Handling Application Block, Logging Application Block, Policy Injection Application Block, Validation Application Block, and Unity Application Block. These blocks are designed to encapsulate recommended practices which facilitate consistency, ease of use, integration, and extensibility. The release also addresses the needs of those who would like to port their existing LOB applications, that already leverage Enterprise Library, to Silverlight

This release includes:

  • Caching Application Block with support for:
    • In-memory cache
    • Isolated storage cache
    • Expiration and scavenging policies
    • Notification of cache purging
  • Validation Application Block with support for:
    • Multi-level complex validation
    • Attribute-based specification of validation rules
    • Configuration-based specification of validation rules
    • Simple cross-field validation
    • Self-validation
    • Cross-tier validation (through WCF RIA Services integration)
    • Multiple rule-sets
    • Meta data type for updating entities with external classes in Silverlight
    • Rich set of built-in validators
  • Logging Application Block, including:
    • Notification trace listener
    • Isolated storage trace listener
    • Remote service trace listener with support of batch logging
    • Implementation of a WCF Remote logging service that integrates with the desktop version of the Logging Application Block
    • Logging filters
    • Tracing
    • Logging settings runtime change API
  • Exception Handling Application Block, including:
    • Simple configurable, policy-based mechanism for dealing with exceptions consistently
    • Wrap handler
    • Replace handler
    • Logging handler
  • Unity Application Block – a dependency injection container
  • Dependency injection container independence (Unity ships with the Enterprise Library, but can be replaced with a different container)
  • Unity Interception mechanism, with support for:
    • Virtual method interception
    • Interface interception
  • Policy Injection Application Block, including:
    • Validation handler
    • Exception Handling handler
    • Logging handler
  • Flexible configuration options, including:
    • XAML-based configuration support
    • Asynchronous configuration loading
    • Interactive configuration console supporting profiles (desktop vs. Silverlight)
    • Translation tool for XAML config (needed to convert conventional XML configuration files):
      • Standalone command-line tool
      • Config console wizard
      • MS Build task
    • Programmatic configuration support via a fluent interface
  • StockTrader V2 Reference Implementation
Download

Download the "Enterprise Library 5.0 Silverlight Integration Pack.msi" and install it. Alternatively, download specific application blocks from NuGet.
To get the optional configuration tool, install it separately from the Visual Studio gallery or download and install the Microsoft.Practices.EnterpriseLibrary.ConfigConsole.vsix package.