Thursday, April 19, 2007

Configuring log4net at Runtime

If you want to set up log4net in runtime instead of using XML configuration file, you can use the following code. After setting up options, do not forget to call ActivateOptions() method.

private void SetupLog()

{

    log4net.Appender.RollingFileAppender rLog = new log4net.Appender.RollingFileAppender();

    log4net.Layout.PatternLayout pl = new log4net.Layout.PatternLayout();

 

    pl.Header = "=================\r\nStart of Program log\r\n";

    pl.Footer = "=================\r\nEnd of Program log\r\n";

 

    pl.ConversionPattern = "%d [%t] %-5p %c - %m%n";

    pl.ActivateOptions();

    rLog.File = "Program.log";

 

    rLog.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;

    rLog.LockingModel = new log4net.Appender.FileAppender.MinimalLock();

    rLog.AppendToFile = true;

    rLog.Layout = pl;

    rLog.DatePattern = "yyyyMMdd";

    rLog.StaticLogFileName = true;

 

    rLog.ActivateOptions();

    log4net.Config.BasicConfigurator.Configure(rLog);

}

Wednesday, April 18, 2007

Loading UserControl at Runtime and Calling Function in UserControl

If you want to load ASP.NET User Control at runtime and calling methods in the UserControl class (such as filling data in the control), you can use the following code. Use reflection to get method in the UserControl then invoke it.

UserControl uc = null;


uc = (UserControl)LoadControl("UserControl.ascx");

Type ucType = uc.GetType();

MethodInfo mi = ucType.GetMethod("FillData");

string returnValue=(string)mi.Invoke(uc, new object[] { arg1, arg2, arg3});

Using HttpWebRequest & HttpWebResponse to POST Data

If you want to get a web page, use HttpWebRequest to make request, and use HttpWebResponse to get response. The following code demostrates how to make request by POST method, then get response from web server. You can get response content by manipulating stream st.

HttpWebRequest hwr;


hwr = (HttpWebRequest)WebRequest.Create(url);


hwr.Method = "POST";


string postdata = "formpostdata=" + System.Web.HttpUtility.UrlEncode(dataToPost);

 

ASCIIEncoding enc = new ASCIIEncoding();

 

byte[] bytel = enc.GetBytes(postdata);

 

hwr.ContentType = "application/x-www-form-urlencoded";

hwr.ContentLength = bytel.Length;

 

//write to request stream

Stream newstream = hwr.GetRequestStream();

newstream.Write(bytel, 0, bytel.Length);

newstream.Close();

 

hwr.Timeout = 10000;

 

//get response

HttpWebResponse hwresp = (HttpWebResponse)hwr.GetResponse();


Stream st = hwresp.GetResponseStream();

Using .NET to Build a XML DOM

XmlDocument xmldoc = new XmlDocument();

XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");


xmldoc.InsertBefore(xmldec, xmldoc.DocumentElement);


XmlElement xmluser = xmldoc.CreateElement("userdata");

xmldoc.DocumentElement.PrependChild(xmluser);


XmlElement name = xmldoc.CreateElement("name");

name.AppendChild(xmldoc.CreateTextNode("abc123"));

xmluser.AppendChild(name);

 

XmlElement id = xmldoc.CreateElement("id");

id.AppendChild(xmldoc.CreateTextNode("A100000000"));

xmluser.AppendChild(id);

Using .NET to Extract Zip Files

using ICSharpCode.SharpZipLib.Core;

using ICSharpCode.SharpZipLib.Zip;


ZipInputStream s = new ZipInputStream(File.OpenRead(@"c:\test.zip"));

ZipEntry e;

byte[] buf = new byte[4096];

string BaseDir = @"c:\test2\";

ZipNameTransform zt = new ZipNameTransform();

 

if (!Directory.Exists(BaseDir))

    Directory.CreateDirectory(BaseDir);

 

while ((e = s.GetNextEntry()) != null)

{

    if (e.IsFile)

    {

        FileStream fs = File.Create(BaseDir + zt.TransformFile(e.Name));

        StreamUtils.Copy(s, fs, buf);

        Console.WriteLine("Extracting " + zt.TransformFile(e.Name));

    }

    else if (e.IsDirectory)

    {

        Directory.CreateDirectory(BaseDir + e.Name);

    }

}

s.Close();

s.Dispose();

Using .NET to Compress Zip Files

Download http://www.icsharpcode.net/OpenSource/SharpZipLib/

using ICSharpCode.SharpZipLib.Core;

using ICSharpCode.SharpZipLib.Zip;


ZipOutputStream s = new ZipOutputStream(File.Create(@"c:\test.zip"));

byte[] buf = new byte[4096];

s.SetLevel(5);

ZipEntry e;

 

foreach (string fn in filenames)

{

    e = new ZipEntry(fn);

    s.PutNextEntry(e);

    FileStream fs = File.OpenRead(fn);

    StreamUtils.Copy(fs, s, buf);

    fs.Close();

    fs.Dispose();

    Console.WriteLine("Adding " + fn);

}

s.Close();

s.Dispose();