Saturday, November 12, 2011

Hot Fix For Windows 7 Start Menu Search Not Working

Sometimes in windows 7 start menu search will not work. Many blogs will tell you to edit the registry. But it is not recommended. Microsoft released the hotfix for this. Download at: http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=977380&kbln=en-us

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