Friday, March 20, 2009

How to split csv file with regular expression

This function is similar to the string.Split function, except it works with a line from csv file and it using c# Regex class.

protected string[] Split(string s)
{
Regex r = new Regex(",(?=([^']*'[^']*')*(?![^']*'))");
List list = new List();

int sStart = 0;
int nCount = 0;

foreach (Match m in r.Matches(s))
{
list.Add(s.Substring(sStart, m.Index - sStart).Trim('"'));
sStart = m.Index + 1;
}
list.Add(s.Substring(sStart, s.Length - sStart).Trim('"'));

return list.ToArray();
}

Wednesday, February 20, 2008

Add UnInstall ShortCut and functionality to your application

Add following code in your Program.cs

static void Main()
{
string[] arguments = Environment.GetCommandLineArgs();
foreach (string argument in arguments)
{
if (argument.ToLower() == "/u")
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
Process proc = new Process();
proc.StartInfo.FileName = string.Concat(path, "\\msiexec.exe");
proc.StartInfo.Arguments = " /x {616898C6-C696-4803-B548-C10BC3AA4342}";
proc.Start();
return;
}
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

Replace {616898C6-C696-4803-B548-C10BC3AA4342} with your product code (you can find it in the properties of setup project, ProductCode field (do not mis it with the application assembly ProductCode, it wont work)

Now create Uninstall shortcut in the setup project to the project output and add /u argument.

Friday, September 28, 2007

HTTP Compression on IIS 6 Server


1. First enable compression on iis server, Right click on Web Sites, Properties.
Select Services Tab

2. Add gzip as server extention, Right Click on Web Service Extensions. Add a new Web Service Extensions. Add Following file
C:\WINDOWS\system32\inetsrv\gzip.dll




Thursday, September 27, 2007

C# HttpResponse.Filter Property and http commression

You can use that property to modify the HTTP entity body before transmission to the client browser.

If you want to send the http response compressed for example.

First you should determine whether client browser accepts compressed content.

Browsers send this information in http header value Accept-encoding.

if(Request.Headers["Accept-encoding"] != null &&
Request.Headers["Accept-encoding"].Contains("gzip"))
{
}

or

if(Request.Headers["Accept-encoding"] != null &&
Request.Headers["Accept-encoding"].Contains("deflate"))
{
}

Here is the complete code. You can put it in you base page, in OnInit or OnLoad methods for example
if(Request.Headers["Accept-encoding"] != null)
{
if (
Request.Headers["Accept-encoding"].Contains("deflate"))
{
Response.Filter = new DeflateStream(Response.Filter, CompressionMode.Compress, true);
Response.AppendHeader("Content-encoding", "deflate"); }
else if (Request.Headers["Accept-encoding"].Contains("deflate"))
{
Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress, true);
Response.AppendHeader("Content-encoding", "gzip");
}
}


Tuesday, September 25, 2007

Rewrite urls in asp.net (To make them search engine friendly)

Suppose you have urls that looks as that one

http://www.vic.bg/Default.aspx?c=29
http://www.vic.bg/Default.aspx?c=30
http://www.vic.bg/Default.aspx?c=31

but you want to looks like those:

http://www.vic.bg/Police.aspx
http://www.vic.bg/Students.aspx
http://www.vic.bg/Sport.aspx

without having actual pages.

You should add new IHttpHandler in your web.config file which dynamically will transform from one to another.

Step by step:

1. Add new library project.
2. Add new class and inherit PageHandlerFactory. That is .net handler that process .aspx files. We will add logic to rewrite pages and then will pass them to original handler

You can add much complex logic here for example transform one url to another with regular expression.


namespace VicHttpHandlers
{
public class VicRewriteUrlHandlerFactory : PageHandlerFactory
{
VirtualPathTableAdapter vpta = new VirtualPathTableAdapter();

Dictionary _pathMaps = new Dictionary();

public VicRewriteUrlHandlerFactory()
{
Vicove.VirtualPathDataTable dt = vpta.GetData();
foreach (Vicove.VirtualPathRow row in dt)
_pathMaps.Add(row.VirtualPath, row.RealPath);
}
#region IHttpHandler Members
VicRewriteUrlHandler handler = new VicRewriteUrlHandler();

public override IHttpHandler GetHandler(
HttpContext context,
string requestType,
string virtualPath,
string path)
{
if (_pathMaps.ContainsKey(HttpUtility.UrlDecode(context.Request.Url.PathAndQuery)))
{
context.RewritePath(_pathMaps[HttpUtility.UrlDecode(context.Request.Url.PathAndQuery)], false);
virtualPath = context.Request.Path;
path = context.Request.PhysicalPath;
}

return base.GetHandler(context, requestType, virtualPath, path);

}

#endregion
}
}

4. Change you web.config

<httpHandlers>
<add verb="*" path="*.aspx" type="VicHttpHandlers.VicRewriteUrlHandlerFactory, VicHttpHandlers" />